Tuesday, October 18, 2011

UNIX command "sed" examples

Here "sed" stands for "stream edit"
The main use of sed is to replace the string provided

bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g'
In the above example the word LINUX is replaced by UNIX in the file def_new.txt and is displayed.
s stands for substitute.

What if you need replace multiple strings?
bash-3.00$ cat def_new.txt | sed -e 's/LINUX/UNIX/g;s/system/COMPUTER'

sed command can also be used to delete
bash-3.00$ whobuild      pts/2        Oct 17 11:28    (10.222.46.189)
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed 'd'
wont display anything as it deletes everything

bash-3.00$ who | sed '1d'
delete the first line
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '1,2d'
delete the first two lines
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '/build/d'delete only the user build
rmc       pts/10       Jul 12 18:45    (10.209.188.122)
root      pts/12       Oct 18 23:46    (10.203.54.223)


bash-3.00$ who | sed '/rmc/,/build/d'
delete both rmc and build
root      pts/12       Oct 18 23:46    (10.203.54.223)

bash-3.00$ who | sed '1,/rmc/d'root      pts/12       Oct 18 23:46    (10.203.54.223)
delete from line 1 till the line has rmc

No comments:

Post a Comment