Alex400
1
I am using SED to edit a file (called file)
the file contains the word "ERROR"
and I want to use SED to:
- Search for text "ERROR"
If found,
- Append new line with text "hoi"
I tried:
sed 's/ERROR/ a\hoi' file
sed 's/ERROR/ a\ hoi' file
I get all the time the error
sed: 0602-404 Function s/ERROR/ a\ hoi cannot be parsed.
what am I doing wrong??? :wall:
sulti
2
sed "s/ERROR/\nhoi/" file
will it work?
sulti
4
Same error? Because what You did try before could not work - You didn't close "s" command.
sed in ksh works pretty normal...
$ ksh
$ echo "1 2 3" | sed "s/2/\nhoi/"
1
hoi 3
Alex400
5
not working: output
$ echo "1 2 3" | sed "s/2/\nhoi/"
1 nhoi 3
Alex, are you in solaris ?
alister
8
The original attempt does not use the append command correctly. You cannot embed it in the replacement portion of a substitute command.
In subsequent suggestions, the use of \n in the replacement text is a non-portable extension (most commonly found on gnu/linux systems).
To add 'hoi' on a line by itself after a line with the word "ERROR", the simplest way would be to use the append command:
sed '/ERROR/a\
hoi\
' file
You could also accomplish this task with the substitution command:
sed '/ERROR/s/$/\
hoi/' file
Too insert a newline immediately after the word "ERROR" and immediately after "hoi", you can use this substitution:
sed 's/ERROR/&\
hoi\
/' file
My suggestions should be portable. They use a backslash-escaped literal newline to insert a newline. \n won't always work.
Regards,
Alister