Sunday, 7 August 2011

Unix tool : Sed

What is Sed?

This post will help you to get the answer of the above question,The same question persuaded me to understand the concept and also to write this post about what i have learned about Sed.

sed(stream editor) is a tool which allows you to perform large-scale, noninteractive editing tasks. For example, sed can replace all the occurrences of a given word in a file with a different word, or delete all lines containing a target string from a file.Sed is particularly used when working with large files .

Substitution command  S:

Sed has several commands,but the essential command i.e used by most is substitute command : S
The substitute command changes all occurrences of the regular expression into a new value.

To try this command , create a file named "file1.txt" and write something like "sed is fun" in the body of that file,then run the following command in terminal.

      $sed s/fun/cool/ <file1.txt >file2.txt

After you run the command you can see a new file created in the same directory with a name file2.txt , when you open the file you can see that the text is changed to "sed is cool".here the command will substitute the content "fun" in the old file (file1.txt) to "cool" in the new file (file2.txt).

Try the following command those who want to test the working in terminal.
      $echo fun | sed s/fun/cool/

This will output as "cool" in terminal.

      $echo sed is fun | sed 's/fun/cool/'

This will output as "sed is cool" in terminal.

There are four parts to this substitute command:

s                      Substitute command.
/ . . / . . /          Delimiter.
fun                  Regular Expression Pattern Search Pattern
cool                 Replacement string. 

The search pattern is on the left hand side and the replacement string is on the right hand side.

Using "&"as the matched string :

By using the "&" symbol you can match the patterns as you like,

      $echo sed is fun | sed 's/[a-z][a-z][a-z]/(&)/'

This will ouput as " (sed) is fun ",  [a-z] matches single letter ,here 3 [a-z] will match 3 letters like(sed).

You can have any number of  "&"  in the replacement string,like

      $echo sed is fun | sed 's/[a-z][a-z][a-z]/(&&)/'

This will output as "(sedsed) is fun".

If you want to match the entire string then you have to make changes like,

      $echo sed is fun | sed 's/[a-z]* [a-z]* [a-z]*/(&)/'

  It will output as " (sed is fun)".

If you want to display the matched string 3 times then try,

      $echo sed is fun | sed 's/[a-z]* [a-z]* [a-z]*/(&)(&)(&)/'

The output will be "(sed is fun)(sed is fun)(sed is fun)".

If we have a text file like "sed is fun 123",then you should change the code like this to get the whole text inside the bracket.
      $echo "sed is fun 123" | sed 's/[a-z]* [a-z]* [a-z]* [0-9]*/(&)/'

Then the output will be "(sed is fun 123)".

Using \1 to keep part of the pattern:-

If you want to remove the numbers from the above example,then you should try like,

     $echo sed is fun 123 | sed 's/\([a-z]* [a-z]* [a-z]*\).*/\1/'

It will remove the numbers and the output will be "sed is fun".

If you want to switch the words around, you can remember the patterns and change the order around:

     $echo sed is fun 123 | sed 's/\([a-z]*\) \([a-z]*\) \([a-z]*\).*/\3 \2 \1/'

The output will be "fun is sed".

Substitute flags:

You can add additional flags after the last delimiter. These flags can specify what happens when there is more than one occurrence of a pattern on a single line, and what to do if a substitution is found.

      $sed 's/[^ ]*/(&)/' <file1.txt>file2.txt

This will put parenthesis around the first word, i.e the first word in the body of your second file have a parenthesis around it.

If you want it to make change of  every word add a "g" after the last delimiter and use the work-around:

      $sed 's/[^ ]*/(&)/g' <file1.txt>file2.txt

The output will be "(sed) (is) (fun)".

These are some basic commands ,there are several other commands that can be used with Sed.

No comments:

Post a Comment