sed: how to replace line if found or append to end of file if not found?
It's actually quite simple with sed
: if a line matches just copy it to the h
old space then s
ubstitute the value.
On the la$
t line ex
change hold space and pattern space then check if the latter is empty. If it's not empty, it means the substitution was already made so nothing to do. If it's empty, that means no match was found so replace the pattern space with the desired variable=value then append to the current line in the hold buffer. Finally, ex
change again:
sed '/^FOOBAR=/{h;s/=.*/=newvalue/};${x;/^$/{s//FOOBAR=newvalue/;H};x}' infile
The above is gnu sed
syntax. Portable:
sed '/^FOOBAR=/{
h
s/=.*/=newvalue/
}
${
x
/^$/{
s//FOOBAR=newvalue/
H
}
x
}' infile
This can probably be shortened. It's not a single sed command and it also uses grep, but this seems to be basically what you're wanting. It's a single line, and it edits the file in-place (no temp files).
grep -q "^FOOBAR=" file && sed "s/^FOOBAR=.*/FOOBAR=newvalue/" -i file ||
sed "$ a\FOOBAR=newvalue" -i file
Here is a simpler sed
approach, as I don't find sed
hold space
easy to work with. If you are comfortable with hold space
, using don_crissti approach gives additional opportunity to preserve anything from the existing line, but this is usually very rare.
In this approach, you just print all but the line that you want to drop and then at the end, append the replacement.
sed -n -e '/^FOOBAR=/!p' -e '$aFOOBAR=newvalue' infile