I am looking for a way to achieve the following:
A certain directory contains 4 (config) files:
I want my bash script to read in each of the files, one by one. In each file, look for a certain line starting with "params: ". I want to comment out this line and then in the next line put "params: changed according to my will".
I know there are a lot of handy tools such as sed to aid with these kind of tasks. So I gave it a try:
sed -ri 's/params:/^\\\\*' File1.conf
sed -ri '/params:/params: changed according to my will' File1.conf
Questions: Does the first line really substitute the regex params:
with \\
following a copy of the entire line in which params:
was found? I am not sure I can use the * here.
Well, and how would I achieve that these commands are executed for all of the 4 files?
So this command will comment every line beggining by params:
in you files, and append a text in the next line
sed -E -i 's/^(params:.*)$/\/\/\1\nYOUR NEW LINE HERE/g'
the pattern ^(params:.*)$
will match any whole line beggining by params:, and the parenthesis indicate that this is a capturing group.
Then, it is used in the second part of the sed command via \1
, which is the reference of the first capturing group found. So you can see the second part comments the first line, add a line break and finally your text.
You can execute this for all your files simply by going sed -E -i 's/^(params:.*)$/\/\/\1\nYOUR NEW LINE HERE/g' file1 file2 file3 file4
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With