简体   繁体   中英

unix scripting error with sed command

I used the below command to replace the "mppu" word with "hihi" and it was working right.

sed 's/mppu/'`echo "hihi"`'/' memo.cir

but when I was trying the below command

sed 's/mppu/'`echo "hi hi"`'/' memo.cir 

then it gives error as

sed: -e expression #1, char 9: unterminated `s' command.

I really don't why it is giving such error as i just added a space in hihi

the correct syntax will be as follows.

Solution 1st: For simple replacement of a string.

sed 's/old_text/new_text/' Input_file

You need not to mention echo and all there.

Solution 2nd: In case you want to search for a string and then do a substitution of any string then following may help you in same.

sed '/look_for_string/s/old_text/new_test/' Input_file

Solution 3rd: If you want to change multiple/all occurrences of any string into a single line then following may help you too by adding g at last of command.

sed '/look_for_string/s/old_text/new_text/g'  Input_file

Solution 4th: In case you want to do the changes into Input_file itself then following may help you in same.

sed -i '/look_for_string/s/old_text/new_text/g'  Input_file

EDIT1: As per OP, code should replace a variable's value to line then following may help you in same then.

VAL="SINGH"
sed 's/tools/'"$VAL"'/'  Input_file

EDIT2: Adding an example for OP for substituting the nth number of a string if it matched a string. Let's say following is my Input_file.

cat Input_file
31116441
AAA,hi,hi,hi,hji
AAA
AA
BBB
BB

Now to substitute the 3rd occurrence of string(hi) for a line which stars from AAA we could do following then.

sed '/AAA/s/hi/cha_cha_ch_ch_cha/3' Input_file
31116441
AAA,hi,hi,cha_cha_ch_ch_cha,hji
AAA
AA
BBB
BB

You need to quote the output from external command. set -x helps to debug what is going wrong.

$ set -x
# can also use: echo 'asd mppu foo' | sed 's/mppu/'"`echo "hi hi"`"'/'
$ echo 'asd mppu foo' | sed 's/mppu/'"$(echo "hi hi")"'/'
+ echo 'asd mppu foo'
++ echo 'hi hi'
+ sed 's/mppu/hi hi/'
asd hi hi foo

You can see that hi hi is within outer single quotes. Without double quotes, you'd get

$ echo 'asd mppu foo' | sed 's/mppu/'`echo "hi hi"`'/'
+ echo 'asd mppu foo'
++ echo 'hi hi'
+ sed s/mppu/hi hi/
sed: -e expression #1, char 9: unterminated `s' command


Similarly, newlines would cause issue

$ echo 'asd mppu foo' | sed 's/mppu/'"$(printf "hi hi\nabc")"'/'
+ echo 'asd mppu foo'
++ printf 'hi hi\nabc'
+ sed 's/mppu/hi hi
abc/'
sed: -e expression #1, char 12: unterminated `s' command

With variables instead of external command:

$ r='hi hi'
$ echo 'asd mppu foo' | sed 's/mppu/'"$r"'/'
+ echo 'asd mppu foo'
+ sed 's/mppu/hi hi/'
asd hi hi foo
$ echo 'asd mppu foo' | sed 's/mppu/'$r'/'
+ sed s/mppu/hi hi/
sed: -e expression #1, char 9: unterminated `s' command
+ echo 'asd mppu foo'

See Why does my shell script choke on whitespace or other special characters? for more details on this topic

you can use

sed 's/old/new/g' file

if you want to replace variable,you can use the follow tow methods

sed -i 's/XXXXX/${WEEK_DAY}/g' ==> sed -i "s/XXXXX/${WEEK_DAY}/g"
sed -i 's/XXXXX/${WEEK_DAY}/g' ==> sed -i 's/XXXXX/'${WEEK_DAY}'/g'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM