Friday, July 1, 2011

sed oneliner - replace text in file

One liner for use inside of a script where a line, or part of a line, or a file needs to be changed:

Suppose that we are passing the name of a file to edit to this script as the first parameter

OLDVARIABLE="the old variable"
NEWVARIABLE="the new variable"

This will do an in place (-i) edit of the file passed to the sed script (the first parameter $1). The sed script itself will do a substitution (s) of the first instance of OLDVARIABLE with the value of NEWVARIABLE. If you wanted to do this at every instance, add (g) to the end of the sed script /g"
The double qoutes around the sed script are not a typo...they are there because I am using variables instead of text or regex.
And that's my oneliner for today...snuck it right in before midnight.