pavement

Sed

From FreeBSDwiki
Revision as of 00:53, 16 October 2004 by Dave (Talk | contribs)
Jump to: navigation, search

Short for "Streamline Editor", sed allows you to run a file through it and either match or change data without actually editing the file itself. This can be particularly handy if you need to make a lot of similar changes to a file -- e.g., you find out you consistently put in a wrong hostname in a configuration file, and you need to change your conf file to point to the right server....except the same change needs to be made 100 times. sed to the rescue.

Note that the change will only happen on the first instance of the phrase in each line.

The most common usage is to change all instances of a phrase to another; to wit:

dave@samizdata:~% cat sed_testfile
 dave
 dave
 davedave
 jimbo
 dave
 freebsd
dave@samizdata:~% sed s/dave/david/ sed_testfile
 david
 david
 daviddave
 jimbo
 david
 freebsd
dave@samizdata:~% more sed_testfile
 dave
 dave
 davedave
 jimbo
 dave
 freebsd
dave@samizdata:~%

As you can see, the changes were not actually made to the file. I didn't tell it to put the changes anywhere! Easily fixed with the help of redirection:

dave@samizdata:~% sed s/dave/david/ sed_testfile > sed_testfile_new
dave@samizdata:~% more sed_testfile_new
 david
 david
 daviddave
 jimbo
 david
 freebsd
dave@samizdata:~%


To give you another example of how useful sed can be, let's say that you have a DHCP server and want to change a whole segment's range:

samizdata# sed s/10.1.0./10.2.0./ dhcpd.conf > new_dhcpd.conf

Will give you a new_dhcpd.conf file with the changes made. You'll want to check the file by hand to make sure that the changes are correct, but you just saved yourself a lot of typing :)

Personal tools