I’m aware that the following command can be used to recursively replace all instances of a particular string with another:
find /path/to/files -type f -print0 | xargs -0 sed -i ‘s/oldstring/newstring/g’
However, I need to do this only for lines that start with a particular string (“matchstr”).
For example, if a file contained the following lines:
This line containing oldstring should remain untouched
matchstr sometext oldstring somethingelse
I want to have this as output:
This line containing oldstring should remain untouched
matchstr sometext newstring somethingelse
Any suggestions as to how I could proceed would be appreciated.
You could do:
ie
The first
/^matchstr/looks for lines matching that regex, and for those lines thes/old/new/gis performed.