Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed replace a word at a line which begins with a specific pattern using

Tags:

sed

How can I replace a word at a line which begins with a specific pattern on FreeBSD? Consider the following file contents:

this is to test 
that was for test

I want to replace "test" at the line which begins with "this".

like image 645
Jack Mc Lauren Avatar asked Sep 09 '13 08:09

Jack Mc Lauren


1 Answers

In order to perform a replacement for lines starting with this, say:

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

This would replace the word test with something on lines starting with this.

If you want to replace all instances of test on the matching lines, supply the g option to sed:

sed '/^this/ s/test/something/g' inputfile
like image 135
devnull Avatar answered Nov 06 '22 04:11

devnull