John H. Robinson, IV wrote:
> Todd Walton wrote:
> > I want to use sed to take a config file, find an option in the file
> > and change its value, and to leave the file in place. I want a
> > portable, scriptable way to do 'vi settings.config', find setting,
> > edit text, ":wq".
> >
> > I don't know sed at all, I just know that it does this kind of thing.
> > What's the best way for a new person to approach getting a basic
> > understanding of its use?
>
> perl excels at this kind of in-place editing. With sed, you need to
> make a copy of the file, use the copy as an input, then write the
> changes out to the file itself.
>
> % perl -pi -e 's/(key)=.*/$1=new_value/' file.config
> % perl -pi.bak -e 's/(key)=.*/$1=new_value/' file.config
I lied. At least with GNU sed (v 4.1.2 tested) you can specify the -i
flag just as you can with perl to edit the file in place, complete with
optional backup extension:
% sed -i -e 's/\(key\)=.*/\1=new_value/' file.config
% sed -i.bak -e 's/\(key\)=.*/\1=new_value/' file.config
Some differences to note:
o With sed, parenthesis for grouping and later substitution must be
quoted
o When referencing a group delimited by parenthesis, you must use \N
where N is a digit that refers to toe grouping in the RE. With
perl, you can either use \N or $N.
o There is no -p option.
If you notice how the perl and sed formats look very similar, this was a
conscience design decision by Larry Wall when he wrote perl. It has a
lot of sed and awk constructions. I am more used to sed, myself.
Good luck!
-john
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list