3

there is 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 occur "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

Mark
  • 43
  • 3

2 Answers2

1

In second case, escape character '\' is interpreted by your shell. Use the 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 '\' appear only once at each occurrence: missing ones have been interpreted by your shell as an escape character. So sed command does only receive the second '\' of each occurence.

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

As you can see, in second case, all character are sent as is to sed.

Usually you need to mix both type of string delimiter:

  • ' (for special character as'\')
  • " (in order to interpret some shell variables):

Example:

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

It is the behavior of the Bash-shell, loog at the difference of these two outputs:

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

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

Btw., it's the same with vars:

root@Server:~# XX=12
root@Server:~# echo "$XX"
12
root@Server:~# echo '$XX'
$XX
Biber
  • 709
  • 6
  • 19