Reputation: 383
I have a line like in a file and want to add below it . I have been trying with command
sed -e '/</session-config>/a\<security-constraint>\' -i filename
But its not working . the error is :
sed: -e expression #1, char 43: unterminated `s' command
example inputfile
<more></more>
<session-config>20</session-config>
<otherfields>10</otherfields>
after sed command
<more></more>
<session-config>20</session-config>
<security-constraint>
<otherfields>10</otherfields>
Please help.
Thanks.
Upvotes: 0
Views: 284
Reputation: 204578
For clarity, simplity, robustness, portability and every other desirable quality of software, just use awk:
$ awk '{print} index($0,"</session-config>"){print "<security-constraint>"}' file
<more></more>
<session-config>20</session-config>
<security-constraint>
<otherfields>10</otherfields>
Note that the above command is treating both the text you are searching for and the text you are adding as strings and so could not care less what characters they contain (except the string terminating "
of course). The above will work on all modern awks in all OSs.
Upvotes: 0
Reputation: 20032
With another delimiter you do not need backslashes.
The string you find must be put back, so use & for it.
The new string on a new line: I use \n
. When your sed doesn't support it,
use a real new-line.
When you want to add a line, do not use a
but make it part op the replacement string.
sed -e '#</session-config>#&\n<security-constraint>trust_nobody</security-constraint>#' -i filename
Upvotes: 0
Reputation: 212634
Use a different delimiter. Also, some sed require a newline after the a. This should work:
sed -e '\@<session-config>@a\
<security-constraint>\'
Upvotes: 1
Reputation: 782564
You need to escape the /
inside </session-config>
. Otherwise, it's treated as the end of the regular expression to match, and s
is then the beginning of the command to apply to those lines:
sed -e '/<\/session-config>/a\<security-constraint>\' -i filename
Upvotes: 2