With a single input file that only contains comments (starting with #) and VARIABLE=value lines, is it possible to replace a value for a single variable if found and, otherwise, append the pair to the end of file if not found?
My current method works by deleting it in a first pass, then appending it to the end of the file in a second pass, but this method messes up the line ordering (and is also two different commands):
sed -r "/^FOOBAR=.*$/d" -i samefile && sed -r "$ a\FOOBAR=newvalue" -i samefile
Is there anyway to do this, ie. keeping line order, in a single sed line? If some other utility (awk, …) does this, I'ld take it over sed.