Friday 3 August 2012

Sed command examples

A command I use constantly at work is the 'sed' command. This command is used when you want to make changes to a file automatically. Most people who use the 'sed' command use it to replace text (which it does brilliantly) but there is so much more
I'm going to show you a few examples of the awesomeness of this command, if you have never used it before I guaranty you will use it after reading this post.
Lets say we have a file which contains the following text:

"This is my favourite place to learn about Linux. Linux is an opensource OS. Linux has a variety of different flavours."

Search and Replace
Using the 'sed' command we can search and replace. for example if I wanted to replace the word 'Linux' with the word 'Unix' I would do the following:

 sed 's/Linux/Unix/' aFile.txt

This would change the first occurrence of "Linux" to "Unix" as shown below:
"This is my favourite place to learn about Unix. Linux is an opensource OS. Linux has a variety of different flavours."

Replacing the nth occurrence of a word is just as easy:

sed 's/Linux/Unix/2' aFile.txt
This will skip the first occurrence of "Linux" and replace the second one with "Unix".
"This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a variety of different flavours."

If we would like to replace all occurrences of a word then we use the "/g" (Global) option:

sed 's/Linux/Unix/g' aFile.txt
All occurrences of the word "Linux" have now been replaced with "Unix"
"This is my favourite place to learn about Unix. Unix is an opensource OS. Unix has a variety of different flavours."

We can mess about with these options and merge them together to replace all occurrences of a word from a certain point, for example replacing all occurrences of the word "Linux" with the word "Unix" after the first occurrence (the example below will make this a bit clearer): 

sed 's/Linux/Unix/2g' aFile.txt

So after the second occurrence the word "Linux" changes to "Unix".

Add a line after or before a match
Here we can add a line after a match is found. For this we use the "a" command, for example:

sed '/flavours/ a "a new line!"' aFile.txt
and the result is as follows:
"This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a variety of different flavours.
a new line!"

The same thing can work adding a line before a match, we do this using the "i" command.

sed '/flavours/ i "a new line!"' aFile.txt
and the result is as follows:
"This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a 
a new line!
variety of different flavours."

Replace a line
We can search for a match and then replace that whole line using the 'c' command. It pretty much the same format as before:

sed '/flavours/ c "a replaced line!"' aFile.txt
and the result is as follows:
"This is my favourite place to learn about Linux. Unix is an opensource OS. Linux has a 
a replaced line!"

The last thing I'm going to show you is the transform option. This is used to transform lower case letters to upper case letters. We use the 'y' command for this. In this example I'm going to change all the small l's and v's to large l's and v's, and vice versa:

sed 'y/lv/LV/ aFile.txt
"This is my faVourite pLace to Learn about linux. Unix is an opensource OS. linux has a Variety of different fLaVours."

 I hope this helps you guy's with the 'sed' command, it can be very useful once you get to know it. Any questions are welcome.

3 comments:

  1. The last one should probably read:
    sed 'y/lv/LV/' aFile.txt

    That y option is news to me though, nice to have new sed commands to add to the repertoire! I personally use tagged expressions all the time, and are sooooo useful!

    ReplyDelete
  2. Indeed Russ, Sorry about that. I was at work while writing that one, I must have been distracted.

    ReplyDelete
  3. Your Linux tips are good even though I arrived here late. Cheers.

    ReplyDelete