You could combine your two separate sed
commands into one:
sed -i -e 's/""""/" " /g' -e 's/"""/" " /g' file
or even,
sed -i 's/"\{3,4\}/" " /g' file
(but only because """"
is being replaced before """
; had you done the substitutions in the opposite order, then this last command would not be equivalent).
You could turn this into a shell script,
#!/bin/sh
sed -i -e 's/""""/" " /g' -e 's/"""/" " /g' -- "$@"
... or into a shell function (in your ~.bashrc
file, for example),
call_sed () {
sed -i -e 's/""""/" " /g' -e 's/"""/" " /g' -- "$@"
}
The --
in the command stops command line option processing and allows you to use the script or function on filenames starting with a dash. The "$@"
will expand to a list of filenames given to the script or function, each individually quoted.
You could use the script or function like so:
$ call_sed EXPERIMENT-*.FILE
With a single sed
command like this, you could even make it an alias, but the quoting needs to be handled with care and I would recommend using a shell function instead:
alias call_sed='sed -i -e "s/\"\"\"\"/\" \" /g" -e "s/\"\"\"/\" \" /g" --'
To make the -i
option (which you may not want to use always) an option to the script:
#!/bin/sh
unset in_place
if [ "$1" = "-i" ]; then
in_place=true
shift
fi
sed ${in_place:+-i} -e 's/""""/" " /g' -e 's/"""/" " /g' -- "$@"
The ${in_place:+-i}
would expand to -i
if $in_place
is non-empty. The variable would be non-empty if the first argument given to the script is -i
.
The exact same modification could be made to the shell function variant of the code to make the shell function take an -i
option.
exit 0
? It would hide the exit status of the (last)sed
call. – Kusalananda Jul 30 '21 at 06:26