4

Some problems with sed

 3 years ago
source link: https://www.codesd.com/item/some-problems-with-sed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Some problems with sed

advertisements

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


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/}'




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK