Tuesday, February 17, 2009

sed

Following is the list of commands that can be used to get your work done using sed.
No doubt there are many online resources available for these but still........

1. Search and replace
sed -e "s/old/new/" file
The above command will search for "old" in "file" and replace it with "new" and dump the output on stdout.

If your sed supports in line processing then
sed -i "s/old/new/" file
in the above case the output would be the file itself

For global search and replace (I am still trying to find out where it is going to be used or how it works ..)
sed -e "s/old/new/g" file
sed -i "s/old/new/g" file

If you want to use back reference, then you can do like this
sed -i -e 's/\(title.*\)/#\1\n title my_linux /' grub.conf
The above will search for a line containing the word 'title', then it will replace the line with
#"the line contain the word 'title' "
add a new line with 'title my_linux'
\1 - gives the first grouped string

No comments:

Post a Comment

Followers