Skip to main content
added 762 characters in body
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87
$ cat config key1 value1 key2 value2 key3 value3 $ sed 's/key2^key2 value2value2$/key2 newvalue2/' config key1 value1 key2 newvalue2 key3 value3 

but beware: if there are more key2 value2 lines (possibly in other sections of the config file) then all will be replaced. this is hard to prevent in sed (possible but hard) and easier in awk. see below for an awk command that respects sections.

explanation:

this sed command does roughly the following:

for every line: if line is "key2 value2": print "key2 newvalue2" 

this sed command s/pattern/replace/ means: in every line search for pattern and if found replace with replace. pattern can be a normal string or a regex (regular expression).

the ^ and & in the regex are called anchors and means beginning of line and end of line respectively. without the anchors this pattern key2 value2 would also match this line xkey2 value2x and the results would be xkey2 newvalue2x.

here are some examples how we can change the behaviour with the pattern.

also works with key=value syntax

you can do a lot more with the correct regex (regular expression). but that would be another answer for another question.

$ cat config key1 value1 key2 value2 key3 value3 $ sed 's/key2 value2/key2 newvalue2/' config key1 value1 key2 newvalue2 key3 value3 

but beware: if there are more key2 value2 lines (possibly in other sections of the config file) then all will be replaced.

also works with key=value syntax

you can do a lot more with the correct regex (regular expression). but that would be another answer for another question.

$ cat config key1 value1 key2 value2 key3 value3 $ sed 's/^key2 value2$/key2 newvalue2/' config key1 value1 key2 newvalue2 key3 value3 

but beware: if there are more key2 value2 lines (possibly in other sections of the config file) then all will be replaced. this is hard to prevent in sed (possible but hard) and easier in awk. see below for an awk command that respects sections.

explanation:

this sed command does roughly the following:

for every line: if line is "key2 value2": print "key2 newvalue2" 

this sed command s/pattern/replace/ means: in every line search for pattern and if found replace with replace. pattern can be a normal string or a regex (regular expression).

the ^ and & in the regex are called anchors and means beginning of line and end of line respectively. without the anchors this pattern key2 value2 would also match this line xkey2 value2x and the results would be xkey2 newvalue2x.

here are some examples how we can change the behaviour with the pattern.

also works with key=value syntax

you can do a lot more with the correct regex. but that would be another answer for another question.

added 215 characters in body
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87

to alsojust match key regardless of old value

$ sed 's/^key2=.*/key2=newvalue2/' config 

to remove possible comment sign

you can do a lot more with the correct regex (regular expression). but that would be another answer for another question.

to also remove possible comment sign

to just match key regardless of old value

$ sed 's/^key2=.*/key2=newvalue2/' config 

to remove possible comment sign

you can do a lot more with the correct regex (regular expression). but that would be another answer for another question.

added 23 characters in body
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87
$ sed 's/key2^key2 value2value2$/key2 newvalue2/' config 

also works with key=value syntax

$ sed 's/key2=value2^key2=value2$/key2=newvalue2/' config 
$ sed 's/#\^#\?key2 value2value2$/key2 newvalue2/' config 
$ sed 's/#\^#\?key2 value2value2$/key2 newvalue2/ w /dev/stderr' config > newconfig 
/\[section2\]^\[section2\]$/ { print insection2=1 next } insection2 && /#^#?key2=value2key2=value2$/ { print "key2=newvalue2" next } /\[^\[.*\]*\]$/ { insection2=0 } 1 
insection2 && /#^#?key2=value2key2=value2$/ { print "key2=newvalue2" print "changed line "NR > "/dev/stderr" next } 
$ sed 's/key2 value2/key2 newvalue2/' config 

also works with key=value

$ sed 's/key2=value2/key2=newvalue2/' config 
$ sed 's/#\?key2 value2/key2 newvalue2/' config 
$ sed 's/#\?key2 value2/key2 newvalue2/ w /dev/stderr' config > newconfig 
/\[section2\]/ { print insection2=1 next } insection2 && /#?key2=value2/ { print "key2=newvalue2" next } /\[.*\]/ { insection2=0 } 1 
insection2 && /#?key2=value2/ { print "key2=newvalue2" print "changed line "NR > "/dev/stderr" next } 
$ sed 's/^key2 value2$/key2 newvalue2/' config 

also works with key=value syntax

$ sed 's/^key2=value2$/key2=newvalue2/' config 
$ sed 's/^#\?key2 value2$/key2 newvalue2/' config 
$ sed 's/^#\?key2 value2$/key2 newvalue2/ w /dev/stderr' config > newconfig 
/^\[section2\]$/ { print insection2=1 next } insection2 && /^#?key2=value2$/ { print "key2=newvalue2" next } /^\[.*\]$/ { insection2=0 } 1 
insection2 && /^#?key2=value2$/ { print "key2=newvalue2" print "changed line "NR > "/dev/stderr" next } 
added 199 characters in body
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87
Loading
added 1932 characters in body
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87
Loading
Source Link
Lesmana
  • 28.1k
  • 20
  • 85
  • 87
Loading