Assuming using bash, having a configuration file like:
- param-a=aaaaaa
- param-b=bbbbbb
- param-foo=first occurence <– Replace
- param-c=cccccc
- # param-foo=first commented foo <– Commented: don’t replace
- param-d=dddddd
- param-e=eeeeee
- param-foo=second occurence <– Rreplace
- param-foo=third occurence <– Last active: don’t replace
- param-x=xxxxxx1
- param-f=ffffff
- # param-foo=second commented foo <– Commented: don’t replace
- param-x=xxxxxx2
In which you can find multiple commented or uncommented lines of the param-foo,
how can you comment all the uncommented param-foos except the very last active one,
resulting in:
- param-a=aaaaaa
- param-b=bbbbbb
- # param-foo=first occurence <– Replaced
- param-c=cccccc
- # param-foo=commented foo <– Left
- param-d=dddddd
- param-e=eeeeee
- # param-foo=second occurence <– Replaced
- param-foo=third occurence <– Left
- param-x=xxxxxx1
- param-f=ffffff
- # param-foo=second commented foo <– Left
- param-x=xxxxxx2
Two parts of the question:
1. How to do it with only one known repeating param?
(only param-foo in the example above)
2. How to do it with all multiple active params at once?
(param-foo + param-x in the example above)
Attention: In this case I don’t know previously the name of the repeating params!
Thanks
If awk is acceptable, this will do it for param-foo and param-x:
You may use a single parameter: p=param-x or add more parameters separated by spaces: p=’param-1 param-2 … param-n’.
Edit: I’m assuming the real input file looks like this:
Let me know if it’s different.
Second edit: providing a solution for mawk users:
Adding solution for the latest requirement: