Linux – sed: how to replace line if found or append to end of file if not found

awkcommand linelinuxsed

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.

Best Answer

  • It's actually quite simple with sed: if a line matches just copy it to the hold space then substitute the value.
    On the la$t line exchange 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, exchange 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
    
  • Related Question