$ 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.