-1

I am trying to append a new line using sed, it works only when I add the new line like this: \\\n:

echo "sometextutf8_filesystemmmmm" | sed -r "/utf8_filesystem/a \\\n# Passive mode"

the output:

sometextutf8_filesystemmmmm

# Passive mode

One or two back slashes do not work! With one \n or two \\n back slashes I just get this output:

sometextutf8_filesystemmmmm
n# Passive mode

without any new line.

Even though, it works properly without having three backslashes with substitution:

echo "sometextutf8_filesystemmmmm" | sed -r "s/utf8_filesystem/\n# Passive mode/"

Output:

sometext
# Passive modemmmm

Could some one explain that behavior?

Mohammed Noureldin
  • 1,245
  • 2
  • 19
  • 29
  • @anubhava: `sed (GNU sed) 4.2.2` – Mohammed Noureldin Sep 12 '17 at 11:21
  • Then `sed -r '/utf8_filesystem/a\\n# Passive mode'` should also work. – anubhava Sep 12 '17 at 11:35
  • @anubhava, yes I already tried that. Actually what I want is an explanation. Why do I need three or two back slashes? what am I trying to escape? – Mohammed Noureldin Sep 12 '17 at 11:43
  • If you want to avoid double escaping then use: `sed -r 's/utf8_filesystem/&\n\n# Passive mode/'` – anubhava Sep 12 '17 at 11:48
  • 1
    https://stackoverflow.com/questions/2369314/why-does-sed-require-3-backslashes-for-a-regular-backslash#2372693 – xenoid Sep 12 '17 at 12:36
  • @xenoid, sorry, I tried to understand that but it is still not clear, could you use that for my situation?. So why do I need for three, not only two backslashes? The two will become one -> `\n` -> I would expect new line, but it is not. it needs to be `\\\n` to work, why is this? or in case of using single quotes, see please my comment bellow in RedGrittyBrick answer. – Mohammed Noureldin Sep 12 '17 at 13:27
  • I think you need three backslashes because you're using double quotes. That way, the shell itself will see the single or double backslashes and will interpret them. With single quotes you need two backslashes. If you try breaking the script in two, sed prints a warning `sed -r -e '/utf8_filesystem/a' -e '\n# Passive mode' << – Paulo Sep 12 '17 at 19:18

2 Answers2

0

I can't reproduce your problem using bash.

$ echo aaa | sed -r 's/aaa/AAA \n#Passive mode/'
AAA
#Passive mode

$ myprog=sed

$ echo aaa | $myprog -r "s/aaa/AAA \n#Passive mode/"
AAA
#Passive mode

so I suspect there is more to this than is stated in the question.


It's probably because

1) you probably have some complicated set of scripts and/or aliases and/or environment variables that cause your arguments to be evaluated multiple times and

2) you are using the wrong type of quotes. When you use "Params" the shell will process escapes converting \\ to \ .

You may be happier using single quotes: 'params'

compare

$ echo "s/aaa/AAA \\\n#BBB/"
s/aaa/AAA \\n#BBB/

$ echo 's/aaa/AAA \\\n#BBB/'
s/aaa/AAA \\\n#BBB/
RedGrittyBrick
  • 78,058
  • 17
  • 128
  • 196
  • It is not the wrong quotes, in my real situation, I am passing a parameter to the command `$some_thing`, therefore I need the double quotes. But I still do not understand why I need `\\n`, normally when I want to add new line, I simply use `\n`. – Mohammed Noureldin Sep 12 '17 at 12:05
  • anyway, if I use your case with single quotes: `'\\n'`, why do I need for two back slashes? why not only one? – Mohammed Noureldin Sep 12 '17 at 13:29
-1

You must use a single quote. When you use double quote, the special characters are not taken literally so whenever you use a backslash you must skip it with another backslash. When you use a single quote things are taken literally so you don't to skip special characters "spaces, dollar signs...etc.".

You may google to read more about differences between single and double quotes in a Linux Bourne-Again shell.

  • is it not a must, I want to pass variables to `sed`, so I need double quotes. – Mohammed Noureldin Oct 03 '17 at 09:41
  • Sorry if I miss something. I don't think it matters if you use single or double quote with sed as long as you take your precautions of using escape characters appropriately. I read in some other thread (and it is obvious in your case) that both '\' and 'n' need to be escaped for your command to work as you expect. You may check here: https://stackoverflow.com/questions/723157/how-to-insert-a-newline-in-front-of-a-pattern – Amjad Abdullah Oct 04 '17 at 14:11
  • Actually it does matter, you can read this here https://superuser.com/questions/1249828/why-does-echo-e-some-text-show-only-one-backslash?noredirect=1&lq=1 but the behaviour of three backslashes is still weird for me. – Mohammed Noureldin Oct 04 '17 at 15:00
  • Thanks for pointing me to the useful thread. It seconds what I was saying as it shouldn't matter about using the quotes **as long as you take your precautions of using escape characters appropriately** The link of your question answers your concern exactly. I don't know why you are still having doubts. My doubt though is why there are 2 different translations by bash itself and by echo while echo is a shell built-in? I was assuming that the bash translation should exactly be treated by echo. But it seems my assumption is not valid especially it contradicts with the practical behavior. – Amjad Abdullah Oct 08 '17 at 08:17