substitution of a word with two words and a space using sed - linux

I am trying to substitute a word using sed with two words. For example I'm using:
sed s/TITLE/New Title/ old.txt > new.txt
However, when I run the command the following populates:
sed: -e expression #1, char 17: unterminated `s' command
Any help would be of great appreciation. I've searched everywhere without any clarity.

You are missing quotes. You need to wrap the substitution portion inside quotes like:
sed 's/TITLE/New Title/' old.txt > new.txt
If you are using variables as part of substitution, you'll need to use double quotes " instead of single quotes ' to allow variables to interpolate.
Take a look at sed man page and explore -i option which allows you to make in-place changes.

Related

Sed command fails with an unterminated `s' command as part of shell script, but not when run separately [duplicate]

I run below sed command
sed -i s/abc=.*$/abc=def ghi/g hpq_sf_attach_wf_param.txt
and it gave me error:
sed: -e expression #1, char 17: unterminated `s' command
I noticed it is due to space in between of def and ghi.
Any idea how to fix it?
You need to use quoting to protect special characters, including spaces, $, and *.
sed -i 's/abc=.*$/abc=def ghi/g' hpq_sf_attach_wf_param.txt
So geekosaur had it right. The the reason you had the problem though is because it needs to be double quotes for the wildcards because with single quotes it takes them as literal characters, not for the meaning you want.
sed -i "s/abc=.*$/abc=def ghi/g" hpq_sf_attach_wf_param.txt
Also if the space between "def" and "ghi" gives you problems, adding a "\" should help making it read it as a literal space.
sed -i "s/abc=.*$/abc=def\ ghi/g" hpq_sf_attach_wf_param.txt

sed command to replace 'root'#'localhost' with 'root'#'%'

I am trying to use sed to replace the expression 'root'#'localhost' with 'root'#'%' with no success. Could someone please help me with the command?
I tried the following:
sed -i ’s#\’root\’#\’localhost\’#\’root\’#\’%\’#g’ xyz.sql
sed: -e expression #1, char 1: unknown command: `?'
sed -i -e ’s/localhost/%/g’ xyz_2616.sql
sed: -e expression #1, char 1: unknown command: `?'
First, make sure you're using a single quote. ’ (a.k.a. ’ or unicode 8217) is not the same as ASCII character 39, '.
Next, you can't escape single quotes inside single quotes. Here's an answer I wrote about that some time ago.
You can, however, put single quotes inside double quotes, or escape them outside your single quoted string. For example, either of the following might work:
sed -e "s/'root'#'localhost'/'root'#'%'/g" file.sql
or
sed -e 's/'\''root'\''#'\''localhost'\''/'\''root'\''#'%'\''/g' file.sql
Alternately, you could substitute just the portion you're interested in, trusting that it doesn't appear elsewhere on the same line:
sed -e '/root.#.localhost/s/localhost/%/' file.sql
The ’ character doesn't look like a single quote (') to me. Make sure that you are using single quotes.
The character you're actually typing is a "right single quote mark". What we refer to as a "single quote" is actually an "apostrophe".
The following should work:
sed -e 's/localhost/%/g' rice_2616.sql
or, your first alternatives but with double quotes to avoid having to escape the embedded single quotes (which I presume are apostrophes):
sed -e "s/'root'#'localhost'/'root’#'%'/g" rice_2616.sql

How do I find and replace text using sed using ~ as delimiter

Good-day,
In a Bash shell script I'm putting together, I am trying to find this text: /usr/local/freeswitch/log/freeswitch.log and replace it with: /var/log/freeswitch/freeswitch.log in this file: /etc/fail2ban/jail.local
This is what I have tried so far, both of which result in the error: sed: -e expression #1, char 75: unterminated `s' command
Attempt #1
sed -i 's~usr/local/freeswitch/log/freeswitch.log~var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
Attempt #2
sed -i 's~usr/local/freeswitch/log/freeswitch.log/var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
My research shows that since the text I'm searching for includes the "/" character, I should be using a different delimiter "~" to separate the find and replace strings. But looks like I'm doing something wrong, any assistance would be appreciated, thanks.
The structure of a sed substitution command is s/PATTERN/REPLACEMENT/ (note the delimiter at the end of the command).
You're right, you can change the delimiter to a different character, so if you're going to use ~ you need to put one of those at the end of the command.

sed is replacing matched text with output of another command, but that command's output contains expansion characters [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 6 years ago.
I'm trying to replace text in a file with the output of another command. Unfortunately, the outputted text contains characters bash expands. For example, I'm running the following script to change the file (somestring references output that would break the sed command):
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt
sed fails with this error:
sed: -e expression #1, char 30: unknown option to `s'
I think bash is substuting the contents of $somestring when building the sed command, but is then trying to expand the resulting text. I can't put the entire sed script in single quotes, I need bash to expand it the first time, just not the second. Any suggestions? Thanks
here the forward slash / is the problem. If it's the only issue you can set sed to use a different delimiter.
for example
$ somestring="abc/def"; echo xxx | sed 's/xxx/'"$somestring"'/'
sed: -e expression #1, char 11: unknown option to `s'
$ somestring="abc/def"; echo xxx | sed 's_xxx_'"$somestring"'_'
abc/def
you also need to worry about & and \ chars and escape them if can appear in the replacement text.
If you can't control the the replacement string, either you have to sanitize with another sed script or, alternatively use r command to read it from a file. For example,
$ seq 5 | sed -e '/3/{r replace' -e 'd}'
1
2
3slashes///1ampersand&and2backslashes\\end
4
5
where
$ cat replace
3slashes///1ampersand&and2backslashes\\end
You have several errors here:
the string somestring has characters that are significative for sed command (the most important being '/' that you are using as a delimiter) You can escape it, by substituting it with a previous
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
that will convert your / chars to \/ sequences.
you are using sed '0,/^title/s/^title*/'"$somestring"'\n&/' $HOME/example.txt which is looking to substitute the string titl followed by any number of e characters by that $somestring value, followed by a new line and the original one. Unfortunately, sed(1) doesn't allow you to use newline characters in the pattern substitution side of the s command, but you can afford the result by using the i command with a text consisting of you pattern (preceding any new line by a \ to interpret it as literal):
Finally the script leads to:
#!/bin/bash
somestring='$6$sPnfj/lnXwZVrec7$fCnL9uy1oWIMZduInKTHBAxhsQxGCsBpm2XfVFFqDPHKidrd93yfjbYvKgYexXHVcvkKdu9lbfy16Ek5GvKy/1'
somestring=$(echo "$somestring" | sed -e 's/\//\\\//g')
sed '/^title/i\
'"$somestring\\
" $HOME/example.txt
If your shell is Bash, you can use parameter substitution to replace the problematic /:
somestring="{somestring//\//\\/}"
That looks scary, but is easier to understand if you look at the version that replaces x with __:
somestring="${somestring//x/__}"
It might be easier to use (say) underscore as the delimiter for your sed s command, and then the substitution above would be
somestring="${somestring//_/\\_}"
If you already have backslashes, you'll need to first replace those:
somestring="${somestring//\\/\\\\}"
somestring="{somestring//\//\\/}"
If there were other characters that needed escaping (e.g. on the search side of s///), then you could extend the above appropriately.
This URL provides the cleanest answer:
Command to escape a string in bash
printf "%q" "$someVariable"
will escape any characters you need escaped for you.

Unterminated `s' command with sed troubleshooting

I have a problem with sed. I want to replace the entire specific line number for multiple lines in multiples documents.
This the bash command for 1 specific line in 1 specific document:
BNAME=$(basename $FILE .pdb)
psfgen1="pdb ./sedpdb/${BNAME}.pdb/"
sed -i '8s/'.*'/'${psfgen1}'/' ./psfgen.inp
And I get this error :
sed: -e expression #1, char 60: unterminated `s' command
Is anyone know how to solve this issue? Thanks!
I can see two things wrong:
There are forward slashes in the string that you're attempting to use in the sed command. These will be interpreted as part of the command, so you should use a different delimiter.
The * is unquoted, so will be glob-expanded by the shell to the names of all the files in the directory.
Reliably using shell variables in string substitutions is non-trivial but can be done using one of the approaches shown in the answers to this question.
In your case, it looks like you can probably get away with using another character as the delimiter, such as #:
sed -i "8s#.*#${psfgen1}#" ./psfgen.inp

Resources