Here we'll see how it can be used
1.Usage
sed [options] 'instruction[s]' file[s]
2. Print the first line of the file
[bsurnida@localhost ~]$ sed -n '1 p' animals.txt
i hate cats
[bsurnida@localhost ~]$ cat animals.txt
i hate cats
ther is a cat
cat is not here
3.Print selected range of lines from a file
[bsurnida@localhost ~]$ sed -n '1,3 p' animals.txt
i hate cats
ther is a cat
cat is not here
4.Print last line of the file
[bsurnida@localhost ~]$ sed -n '$p' animals.txt
elephant is the biggest animal
5.exclude a set of lines
[bsurnida@localhost ~]$ sed -n '1,3!p' animals.txt
a cat cant exits
here is a black cAT
no he is not taking cat exam
6.print lines containing a word (something like grep)
[bsurnida@localhost ~]$ sed -n '/cat/p' animals.txt
i hate cats
ther is a cat
cat is not here
a cat cant exits
7. Delete the blank lines from the document
[bsurnida@localhost ~]$ sed -e '/^$/d' animals.txt
Option d for delete
8.redirect the output to a new file
bsurnida@localhost ~]$ sed -e '/^$/d' animals.txt > new_animals.txt
9.search and replace using sed
bsurnida@localhost ~]$ sed -ne 's/
Option n for silent output , no extra lines in the new file
10.In place editing of a file and file backup
bsurnida@localhost ~]$ sed -i.bak -e 's/3/4' sed1.txt
1 comment:
Nice Article:
For more examples on SED: Unix SED Examples
Post a Comment