Friday, December 12, 2014

SCRIPTING:: Linux sed tool

sed is Stream Editor and a fabulous stram editor. It can do wonders for linux programmers working in scripting domain. For the professionals in semiconductor industry, it's an elixir at times.

Some of the most common things that can be done are as follows -

1./    s/..../..../ To Substitute


To replace all instance of black with white:

$ sed s/black/white/ new

There's some particular need to replace the output of a previous command in script. 

   #! /bin/csh -f

   foreach word ( `/bin/ls *.do` )
        sed 's/$1/$2/g' $word > /tmp/a
        \cp -rf /tmp/a $word
   end

2/   d and D   To Delete

To look at the first 20 lines of a file, you can use:
$ sed '21,$ d' 
Deleting all lines that start with a "#" is easy:
$ sed '/^#/ d'
3/ i \ - Insert
You can insert a new line before the pattern with the "i" command: 
#!/bin/sh
sed '
/TEST/ i\
Add this line before every line with TEST'

No comments: