0

I have a very simple text file here with the following contents

line1
line2
line3
line4

I want to modify the contents via sed(or some other app) so it becomes

line1
line2
#this line was added by sed
line3
line4

So I tried sed -e "s/line2/line2\\n#this line was added by sed/" my-text-file-here.txt, but the output is:

line1
line2\n#this line was added by sed
line3
line4

Any ideas on how to do it correctly? Thank you

1

This is assuming the csh shell:

To simply append a line after another:

% sed '/line2/a\\
# new line here\
' file
line1
line2
# new line here
line3
line4

To insert a line before another:

% sed '/line3/i\\
# new line here\
' file
line1
line2
# new line here
line3
line4

To substitute a line with two new lines using the s command:

% sed 's/line2/&\\
# new line here/' file
line1
line2
# new line here
line3
line4

Tested on OpenBSD 6.1 running sed and csh from the base system.

3

With GNU sed, your code works just fine:

$ sed -e 's/line2/line2\n#this line was added by sed/' file
line1
line2
#this line was added by sed
line3
line4

For BSD sed, however, \n is not treated as a newline in the replacement text. If your shell is bash, a good work-around is to use $'...' to insert the newline:

$ sed -e $'s/line2/line2\\\n#this line was added by sed/' file
line1
line2
#this line was added by sed
line3
line4

In addition to bash, zsh and ksh support $'...'.

Another option is to insert a real newline:

$ sed -e 's/line2/line2\
#this line was added by sed/' file
line1
line2
#this line was added by sed
line3
line4

UPDATE: In csh, this last option requires an additional \:

% sed -e 's/line2/line2\\
#this line was added by sed/' file
line1
line2
#this line was added by sed
line3
line4
  • The second example doesn't work in csh, which is what the OP is using. csh doesn't have $'...'. I also couldn't get your third example to run in csh (#this lin ...: unescaped newline inside substitute pattern). csh has funky string handling. – Kusalananda Sep 7 '17 at 6:23
  • Thank you for that info. "The second example doesn't work in csh..." The text clearly states that the second example was for bash. $'...' is not POSIX but it is supported by bash, ksh, and zsh. The info that the OP was using csh was added to the question 2 hours after I wrote my answer. – John1024 Sep 7 '17 at 6:39
  • @Kusalananda Since I wrote my comment, the question was modified again this time removing mention csh. – John1024 Sep 7 '17 at 6:47
  • 1
    I missed the mentioning of Bash there, sorry! The edit was done by @AFSHIN, not by the owner of the question. – Kusalananda Sep 7 '17 at 6:54
  • @Kusalananda I see that you have edited the question to restore the info. Very good. – John1024 Sep 7 '17 at 7:17
2

Looks like you want the append command, really. Using Bash or any shell that supports $'\n' (most do):

sed $'/line2/a\\\n#this line was added by sed\n' file.txt

Or, more readably, using a sed commands file:

/line2/a\
#this line was added by sed

To show the full method:

$ cat file.txt 
line1
line2
line3
line4
$ cat sedfile 
/line2/a\
#this line was added by sed
$ sed -f sedfile file.txt 
line1
line2
#this line was added by sed
line3
line4
$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.