1

I want to ask 2 questions about sed.

For example, when I try to put a string to sed which contains special character like (.\n*.+) and sed cannot run properly.

set a = ".\n*.\+"
set input = ".\n*.\+adsdfasdf"

Then execute:

echo "$input" | sed 's/'$a'/hi/g' # It will give: sed: No match

but

echo "$input" | sed "s#${a}#hi#g" # It will run but not true

My questions are:

  1. What is the difference between these commands: sed 's///' and sed "s###"
  2. How to treat input just as it is purely string?
TuanLaDaPoet
  • 43
  • 1
  • 9
  • both gives `hi` as result for me on bash shell and GNU sed `a=".\n*.\+"; input=".\n*.\+adsdfasdf"; echo "$input" | sed 's/'$a'/hi/g'` .... see https://stackoverflow.com/documentation/sed/1096/substitution#t=201702190808137883167 for some explanation on using different delimiters.. – Sundeep Feb 19 '17 at 08:10
  • afaik, there is no way to tell sed that string is to be treated as literal, you'll have to escape the meta-characters as well as the delimiter used – Sundeep Feb 19 '17 at 08:14

3 Answers3

0

1. What is the difference between these commands: sed 's///' and sed "s###"
--- In your case, / or # is a separator of the crucial "sections":

The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ‘s/regexp/replacement/flags’.
...
The / characters may be uniformly replaced by any other single character within any given s command.


2. How to treat input just as it is purely string?
--- To expand/interpolate a variables within sed command, those variables OR the whole expression should be enclosed with double quotes:

set a = ".\n*.\+"
echo "$input" | sed "s/$a/hi/g"
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Well, you have a \n in the input string, which you substitute without quote delimiters in 's/'$a'/hi/g' and is parsed as a space, so you pass actually two parameters to sed(1) and you substitute as only one parameter (with the \n included as one character) in only one parameter in "s#$a#hi#g" (in which double quotes include the variable substitution). There is actually no difference in the character used as delimiter in sed(1), but you have called it differently in the two calls.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
-1

Finally, I found out that the separator MUST BE different with any characters in the string! Or it will cause error!

Thus, now I will change the separator to #. It's like comment character.

Aamir
  • 16,329
  • 10
  • 59
  • 65
TuanLaDaPoet
  • 43
  • 1
  • 9
  • It has been seen that when the sed delimiter is set to / and your patterns also contain / you need to escape them. By selecting a different separator (like |, #,etc) that is NOT present in your patterns, you reduce escaping. – George Vasiliou Feb 19 '17 at 16:45
  • Thank you. I finally got it. – TuanLaDaPoet Feb 20 '17 at 02:09
  • Hello, and welcome to Stack Overflow. Please don't post commentary as answers -- only content which actually attempts to answer the question should be posted using the "Post Your Answer" button. A moderator can convert this to a comment, but the usual way to say thanks here is to click the up arrow next to things you like, and maybe the green check mark next to the answer you like the most. – tripleee Feb 20 '17 at 05:54
  • @tripleee I got it! Thank you! – TuanLaDaPoet Feb 23 '17 at 07:22