h1

Search & Replace – sed

June 16, 2006

You can use the sed command to change all occurrences of one string to another within a file, just like the search-and-replace feature of your word processor. The sed command can also delete a range of lines from a file. Since sed is a stream editor, it takes the file given as input, and sends the output to the screen, unless you redirect output to a file. In other words, sed does not change the input file.

The general forms of the sed command are as follows:

Substitution sed 's/<oldstring>/<newstri ng>/g' <file>
Deletion sed '<start>,<end>d' < file>

Let's start with a substitution example. If you want to change all occurrences of lamb to ham in the poem.txt file in the grep example, enter this:

sed 's/lamb/ham/g' poem.txt
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a ham spamwich

In the quoted string, the "s" means substitute, and the "g" means make a global change. You can also leave off the "g" (to change only the first occurrence on each line) or specify a number instead (to change the first n occurrences on each line).

Now let's try an example involving deletion of lines. The values for start and end can be either a line number or a pattern to match. All lines from the start line to the end line are removed from the output. This example will delete starting at line 2, up to and including line 3:

sed '2,3d' poem.txt
Mary had a little lamb
Jill had a lamb spamwich

This example will delete starting at line 1, up to and including the next line containing Jack:

sed '1,/Jack/d' poem.txt
Jill had a lamb spamwich

The most common use of sed is to change one string of text to another string of text. But I should mention that the strings that sed uses for search and delete are actually regular expressions. This means you can use pattern matching, just as with grep. Although you'll probably never need to do anything like this, here's an example anyway. To change any occurrences of lamb at the end of a line to ham, and save the results in a new file, enter this:

sed 's/lamb$/ham/g' poem.txt > new.file

Since we directed output to a file, sed didn't print anything on the screen. If you look at the contents of new.file it will show these lines:

Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich

Use the man sed command for more information on using sed.

4 comments

  1. My spouse and I absolutely love your blog and find nearly all of your post’s to be exactly I’m

    looking for. Does one offer guest writers to write content
    for you

    personally? I wouldn’t mind producing

    a post or elaborating on most of the subjects you write

    in relation to here. Again, awesome site!


  2. My spouse and i felt

    so comfortable that Ervin could round up

    his web research through your precious recommendations he made out of your
    web

    pages. It’s not at all simplistic to just happen to be making a gift of facts that many some other people could have been selling. And we understand we’ve got the blog
    owner to give thanks to because of that. These explanations

    you’ve made, the easy web site menu, the relationships you aid to foster – it’s many astonishing, and it’s really helping our son in addition to the family do think that

    matter is brilliant, and that is

    wonderfully essential. Thank you for the whole lot!


  3. We’re a group of volunteers and starting a new scheme in our community. Your site

    offered us with valuable information to work on. You have done a formidable job

    and our whole community will be grateful to you.


  4. Hi, I think your blog might be having browser compatibility issues.
    When I look at

    your blog in Chrome, it looks fine but when opening in
    Internet Explorer, it

    has some overlapping. I just wanted to give you a quick
    heads up! Other then that, awesome blog!



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: