Sed append line

Is there a sed command to add a line after a matched line? I tried searching, but was a little confused by the results.

I want it to fit

#address=/doubleclick.net/127.0.0.1 

And add a line below it as

 #address=/doubleclick.net/127.0.0.1 address=/anotherurl/ipaddress 

Is it possible?

+4
source share
2 answers

You can use the a (add) command in sed :

 $ sed -i '/^#address=\/doubleclick.net\/127.0.0.1$/a\ > address=/anotherurl/ipaddress' file.txt 

Or you can use the s (substitute) command:

 $ sed -i ' s@ ^#address=/doubleclick.net/ 127.0.0.1$@ &\naddress=/anotherurl/ ipaddress@ ' file.txt 

Note. $ and > are the bash prompt.

+10
source

This might work for you:

 echo "#address=/doubleclick.net/127.0.0.1"| sed '\|#address=/doubleclick.net/127.0.0.1|a\address=/anotherurl/ipaddress' #address=/doubleclick.net/127.0.0.1 address=/anotherurl/ipaddress 

You can use any delimiter you like in the address by adding \ ie \|...| to replace the \ command is not required.

If you need an empty string and then the text following the match, use:

 echo "#address=/doubleclick.net/127.0.0.1"| sed '\|#address=/doubleclick.net/127.0.0.1|a\\naddress=/anotherurl/ipaddress' #address=/doubleclick.net/127.0.0.1 address=/anotherurl/ipaddress 
0
source

Source: https://habr.com/ru/post/1399510/


All Articles