Reputation: 788
I want to be able to change specific settings in a configuration file, which includes removing the comment code (;
) that comment out those settings. Furthermore, I want to preserve real comments. That means that
some_setting=0
or
;some_setting=0
would both become
some_setting=9
But it also means that
some_setting=0 ;an honest-to-goodness comment
or
;some_setting=0 ;an honest-to-goodness comment
would be
some_setting=9 ;an honest-to-goodness comment
I'd also want to leave
;Idle talk about some_setting=
alone.
I seem to be able to achieve almost all of this with
sed -e 's/^\s*;\?\s*some_setting\s*=.*\(;\)\?/some_setting=9 \1/'
but the problem is the real comments get stripped out of lines that have some_setting
. Notice that I've made allowances for there to be whitespace around a lot of these tokens.
I've looked at using awk
for this, but it seems the best bet is sed
.
I'm using GNU sed 4.2.2 on a Debian 8.3 for this.
Upvotes: 0
Views: 35
Reputation: 52102
I used this as test input:
some_setting=0
;some_setting=0
some_setting=0 ;an honest-to-goodness comment
;some_setting=0 ;an honest-to-goodness comment
;Idle talk about some_setting=
Using this command:
$ sed -r 's/^;?(some_setting=)[^[:space:]]*(.*)/\19\2/' infile
some_setting=9
some_setting=9
some_setting=9 ;an honest-to-goodness comment
some_setting=9 ;an honest-to-goodness comment
;Idle talk about some_setting=
So a line starting with an optional ;
followed by some_setting=
and whatever non-space characters come after it is replaced with a line starting with some_setting=9
; the rest of the line stays the same.
Upvotes: 1