Some sed problems

no problem with this command:

sed -i -E '/ChannelSetting=/{:loop /\\/{s/\\//g;N;bloop};s/(ChannelSetting=).*/\1/}' build_config/resource.properties

      

but this command will execute "sed: -e expression # 1, char 30: Unmatched {":

sed -i -E "/ChannelSetting=/{:loop /\\/{s/\\//g;N;bloop};s/(ChannelSetting=).*/\1/}" build_config/resource.properties

      

What's the difference with "and" caused the error? Thanks to

+3


source to share


2 answers


In the second case, the escape character '\' is interpreted by your shell. Use echo command to understand the difference:

>> echo "/ChannelSetting=/{:loop /\\/{s/\\//g;N;bloop};s/(ChannelSetting=).*/\1/}" 
/ChannelSetting=/{:loop /\/{s/\//g;N;bloop};s/(ChannelSetting=).*/\1/}

      

Note that '\' appears only once in each case: missing ones are interpreted by your shell as an escape character. Thus, sed only gets the second "each".

>> echo '/ChannelSetting=/{:loop /\\/{s/\\//g;N;bloop};s/(ChannelSetting=).*/\1/}'
/ChannelSetting=/{:loop /\\/{s/\\//g;N;bloop};s/(ChannelSetting=).*/\1/}

      

As you can see, in the second case, all characters are sent as is for sed.



Usually you need to mix both types of line separator:

  • '(for special character like' \ ')
  • "(to interpret some shell variables):

Example:

myMatch='ChannelSetting='
sed -i -E "/$myMatch/"'{:loop /\\/{s/\\//g;N;bloop};s/('$myMatch').*/\1/}'

      

+1


source


This is the behavior of Bash-shell, loog on the difference between these two outputs:

root@Server:~# echo "\\"
\
root@Server:~# echo '\\'
\\

      

The backslash in "" quotes the next char, the backslash in "" is just a backslash.



Btw., Same with vars:

root@Server:~# XX=12
root@Server:~# echo "$XX"
12
root@Server:~# echo '$XX'
$XX

      

+1


source







All Articles