A simple use of sed

May 26th, 2004

sed and awk are programs I usually get by without, but here's a handy use for sed.

Say you've got a file called johnbrown.txt containing the following text:

This file is the property of John Brown.
John Brown will be very annoyed if anyone alters it.
Signed: John Brown.

Run the following command:

sed -e s/John Brown/Jill Green/g johnbrown.txt > jillgreen.txt

You now have a new file containing the following:

This file is the property of Jill Green.
Jill Green will be very annoyed if anyone alters it.
Signed: Jill Green.

Not much use for a three-line file, but very handy for a 3000-line file.

Find out more about sed at:
http://pegasus.rutgers.edu/~elflord/unix/sed.html

If you want to do anything more complex than this (i.e. match whole words only, or match across multiple lines) then you can do it in sed, but it's painful. You're much better switching over to Windows and using Replace from Eluent Tools.

Potentially similar posts

Leave a comment