Sed replace the word in the line starting with a specific pattern using

How can I do this on FreeBSD? Given the following file contents:

this is to test that was for test 

I want to replace the "test" with a line starting with "this".

+6
source share
2 answers

To replace lines starting with this , say:

 $ sed '/^this/ s/test/something/' inputfile this is to something that was for test 

This will replace the word test with something with lines starting with this .

If you want to replace all instances of test with the corresponding lines, put the g option for sed:

 sed '/^this/ s/test/something/g' inputfile 
+11
source

To make changes in place, use the following command:

 sed -i '/^this/ s/test/something/g' inputfile; 
0
source

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


All Articles