« Cross Site Scripting of the day | Main | Iterating a file using a basic for loop in bash »


Inline editing with sed

This is going to be installment one of quite a few little tricks and tips that I never seem to remember, and consequently alsways end up having to go out an research again. Hence, I'll now be blogging these little gems so they are in one place.

Sed is very useful for editing a stream (i.e. a file) on the fly. The most common thing I use it for is in a script in order to replace a setting in a file.

sed -e '/foo/s//bar/g' - replace all instances of foo (a regex) with bar. Note that the -e is not needed if there is only one "script" to process.

Update - Thanks to mat (see comments) for pointing out my fat fingering - sed -e '/foo/s/bar//g'

Found this useful? Then Digg It.


Comments (1)

mat:


sed -e '/foo/s/bar//g'
you sure about that?
what about
sed -e '/foo/s//bar/g' or
sed -e 's/foo/bar/g'
- s/regular expression/replacement/flags

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

All spam will be reported

About

This page contains a single entry from the blog posted on September 21, 2006 2:11 PM.

The previous post in this blog was Cross Site Scripting of the day.

The next post in this blog is Iterating a file using a basic for loop in bash.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.36

Valid XHTML 1.0!