Home > sed > sed – Replace only certain occurances

sed – Replace only certain occurances

Problem:
I needed to change only certain occurances.

Solution:
Best explained with an example.

To change the first one.

$ echo "little kittens on a little boat" | sed 's/little/big/'
big kittens on a little boat

To change the second one only.

$ echo "little kittens on a little boat" | sed 's/little/big/2'
little kittens on a big boat

To change all the rest starting from the second one.

$ echo "little kittens on a little boat eating little fish" | sed 's/little/big/2g'
little kittens on a big boat eating big fish
Advertisements
Categories: sed
  1. December 16, 2013 at 11:23 am

    how interesting… this just comes very handy 🙂 did not know about it… thanks

  1. No trackbacks yet.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: