Sunday 29 January 2012

sed


sed
sed  or stream editor is an editing utility that modifies  lines of a file as specified by an instruction string and writes it to standard output .sed   can thus  perform  the same functions of the commands like tr, grep  and awk ,even though sed has  functionalities and options unique to itself.
Though the applications of sed  are enormous, only some of the important uses of the command  are discussed here with examples.
Example 1)
It is required to you to print the contents of a file  starting from a particular line number  and  ending into  another ,use sed as follows.
/home/UAT$  sed   -n   ‘35,46p’  general-dates.prt
This  command prints  the contents of the file  “general-dates.prt “  starting from  its 35th line to the  46th.
To print  only the  35th line of a file use
/home/UAT$  sed   -n   ‘35p’  general-dates.prt
More examples
·         To  print  all the lines of a file starting from  its 1st line to  the last line
       sed   -n   ‘1,$p’  general-dates.prt
·         To  print  all the lines of a file between  line  numbers  whose  values are stored  in  variables $start-line  and  $end-line .
sed  –n  “${start-line} , ${end-line}p”   general-dates.prt
·         To  print all the lines of the file except the 5th line
sed    ‘5d’  general-dates.prt
·         To  print all the lines of the file except those lines  between the  8th  and  the 40th (i.e. 8th line and 40th lines are also not printed).
sed    ‘8,40d’  general-dates.prt


Example2)
sed can also  search for patterns in file or standard input just like grep , but much more sophisticated pattern searches can be done  as shown in  the following examples.
·         A  grep  command can be implemented using sed as follows
Consider a grep command
grep   industry  general-dates.prt
alternatively sed can perform  the same using..
sed –n ‘/industry/p’  general-dates.prt

·         To  print  all the lines of a file between the first occurrences of two patterns
     sed –n ‘/industry/,/station/p’   general-dates.prt

This prints all the lines starting from the line which has the first occurrence of  ‘ industry’ and  the first occurrence of  ‘station’ .


·         To print  all the lines of a file  starting from its first line and  the  line of  first occurrence of  ‘industry’,
       sed –n ‘1,/ industry/p’   general-dates.prt



·         To  put all the lines starting from line with the first occurrence of the pattern ‘industry’  in the file  to the last line of  the file  into standard output(i.e. print).

sed –n ‘/industry/,$p’   general-dates.prt



·         To  print all the lines starting from the line containing the  pattern string whose value is stored in  a variable $ptrn  to the 15th line of the file.

sed –n  ‘/’$ptrn‘/,15p’  general-dates.prt

The   pattern space can contain   more complex   regular expressions .


Example3)
sed  can  replace the occurrences  of patterns  into a new one from  a file and put it to standard output.
·         To  replace  the first occurrence of   ‘teacher’   with  ‘tutor’  in the file  groupsList.xml
sed  ‘s/ teacher/ tutor/’  groupsList.xml
·         To replace all the occurrences of   ‘teacher’   with  ‘tutor’  in the file  groupsList.xml
sed   ‘s/ teacher/ tutor/g’  groupsList.xml
here  g -> represents  global.
·         To replace all the occurrences of   a string stored in a variable  ${string_pattern}  to  ${replace_string}   in a file.
sed   ‘s/’${string_pattern}’/’ ${replace_string}’/g’  groupsList.xml

·         To  add a  “,”   character  between  the  original pattern  and  replacement string   defined by ${string_pattern}’   and  ${replace_string}  respectively  .
sed   ‘s/’${string_pattern}’/&,’${ replace_string }’/g’  groupsList.xml
the single quotes enclosing the variable is a necessary.
&  variable stores the matched pattern when the string  stored in ${string_pattern} is a regular expression.




Example 4)
Sed can be used to edit a particular line of the file. amazing..isnt it..?
/home/UAT$ cat dir_struct
1.       35467       vdn          /home/vd2
2.       46788       ghi        /var/gh1/gh
3.       89078       bjk       /home/vd2
4.       56890       lod          /home/lod/inter
5.       33456       bhj       /home/bhind
6.       45790       krk       /myhome/krk

Now if I want to change the  directory structure on line number 4 from /home to /var, sed can be used as follows.

/home/UAT$ sed  ‘4  s/home/var/’  dir_struct
1.       35467       vdn          /home/vd2
2.       46788       ghi        /var/gh1/gh
3.       89078       bjk       /home/vd2
4.       56890       lod          /var/lod/inter
5.       33456       bhj       /home/bhind
6.       45790       krk       /myhome/krk

Here we substituted only  home with var in the 4th line .If you need to substitute /home/vd2  with /var/vd1 , you would have written the command as follows.
sed  ‘4  s/\/home\/vd2/\/var\/vd1/’  dir_struct
looks a little ugly because we used ‘/ ‘ as delimiter and to differentiate   the ‘/’  in /home/vd2 and /var/vd1,  a backslash “\”was used before it.
instead of using ‘/’as delimiter  other characters  like : ,  |  ,  { ,  # , !  etc  can be used for such scenarios.
The above sed can be written using # as follows.

sed  ‘4  s#/home/vd2#/var/vd1#’  dir_struct




Example 5)
You have many occurrences of a pattern in a line and you want to replace   just the nth pattern  or all  n+1,n+2.. occurrences . use  a number after  the ending “/”.
/home/jade$ echo "1st 2nd 3rd 4th 5th 6th"|sed "s/[0-9]th//2"
1st 2nd 3rd 4th  6th                                #  |-----second occurrence of [0-9]th

The command above replaced only the 2nd occurrence of a digit followed by “th”.
If you need to replace all occurrences starting from 2nd,then use the following command.

/home/jade$ echo "1st 2nd 3rd 4th 5th 6th"|sed "s/[0-9]th//2g"
"1st 2nd 3rd 4th  "



No comments:

Post a Comment