简体   繁体   English

sed命令导致unix脚本错误

[英]unix scripting error with sed command

I used the below command to replace the "mppu" word with "hihi" and it was working right. 我使用下面的命令将“ mppu”一词替换为“ hihi”,并且它运行正常。

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 我真的不为什么会出现这样的错误,因为我只是在hihi中添加了一个空格

the correct syntax will be as follows. 正确的语法如下。

Solution 1st: For simple replacement of a string. 解决方案1:用于简单替换字符串。

sed 's/old_text/new_text/' Input_file

You need not to mention echo and all there. 您无需提及echo及其所有内容。

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. 解决方案2:如果您想搜索一个字符串然后替换任何字符串,那么下面的内容可能会帮助您。

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. 解决方案3rd:如果要将任何字符串的多个/所有出现都更改为单行,则以下命令也可以通过在命令末尾添加g来帮助您。

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. 解决方案4:如果您想对Input_file本身进行更改,则以下内容可能会帮助您。

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. EDIT1:按照OP,代码应将变量的值替换为行,然后跟随可能会帮助您。

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. EDIT2:为OP添加一个示例,用于替换与字符串匹配的字符串的第n个数字。 Let's say following is my Input_file. 假设以下是我的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. 现在,将第3次出现的string(hi)替换为从AAA开始加星标的行,然后可以执行以下操作。

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有助于调试出了什么问题。

$ 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. 您会看到hi hi在外单引号内。 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? 请参阅为什么我的shell脚本在空白或其他特殊字符上会阻塞? 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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