Sunday, 17 April 2016

How to use 'sed' command ?


What is sed ?
 1. sed is a Stream EDitor.
 2. reads line by line when a file is feed to it.
 3. searches for a term and replace with desired term in a file/stream


Search and replace (s=search)

sed ‘s/searchterm/replaceterm/’ inputfile
OR
cat inputfile | sed ‘s/searchterm/replaceterm/’
OR
echo “This is test message” | sed ‘s/searchterm/replaceterm/’


Example 1. Search for google in file test.txt and replace with yahoo
sed ‘s/google/yahoo/’ test.txt


Example 2. By default sed will work on only first occurrence.
echo “shaan leads, shyam needs” | sed ‘s/sh/li/’
Output
liaan leads, shyam needs

Solution is g switch, to inform sed to search for the term globally/multiple times within the line.
echo “shaan leads, shyam needs” | sed ‘s/sh/li/g’
Output
liaan leads, liyam needs


Example 3. To search for /var/ftp/pub and replace it with /opt/ftp/com. The below code will not work properly.
sed ‘s//var/ftp/pub//opt/ftp/com/’ test.txt
For this you should change the separator from / to some other char such as # or $ or _
In fact any character which is not present in search and replace term.

For above example this will work.
sed ‘s_/var/ftp/pup_/opt/ftp/com_’ test.txt
Here my separator is _


Example 4. Search for Shaan and replace it with Mr. Shaan use below command
sed ‘s/Shaan/Mr. &/’ test.txt&
for matched character and Shaan is stored in & by default.


Example 5. Only printing of changed lines
–n (Suppress full output) and p (Print the changed lines)
cat hello.txt
shaan car
khan truck
bike shaan
baby corn

cat hello.txt | sed -n 's/shaan/sonali/p'
Output
sonali car
bike sonali


Example 6. Make changes to the file
Use –i for inserting
sed –I ‘s/sonali/shaan/’ hello.txt
Note: Use –i option carefully because you cannot revert back the changes of original file.

With output redirection we can do the same thing
sed ‘s/baby/dady/’ < hello.txt > abc.txt


Example 7. Write only changes to another file
With w option, we will get only changes to the new file
sed ‘s/baby/dady/w abc.txt’ hello.txt


Example 8. Deleting lines (using d)
To delete line number 3 use : 
sed ‘3d’ hello.txt

To delete 4 line and update the original file
sed –i ‘4d’ hello.txt


Example 9. Execute multiple commands (with –e option)
sed –e ‘s/shaan/sonali/’ –e ‘s/bca/mca/’ –e ‘s/baby/dady/’ hello.txt

Instead of -e , you can use ; (semicolon)
sed ‘s/shaan/sonali/;s/bca/mca/;s/baby/dady/’ hello.txt


Example 10. Search in a line and then replace the term in that line alone
sed ‘3 s/shaan/sonali/’ hello.txt

Search from 1st line to 4th line and replace that term
sed ‘1,4 s/shaan/sonali/’ hello.txt

Search for a term from 3rd line to 5th line and replace it only between these lines
sed ‘3,5 s/shaan/sonali/’ hello.txt

Search for a term from 2nd line to the end of the file and replace it with a term
sed ‘2,$ s/shaan/sonali/’ hello.txt


Example 11. Combine two-line in to a single line. (Use N option)
Merge two lines in to single line and it will keep newline character in the combine line.
sed ‘N’ hello.txt

If you want to really see the difference you have to use search and replace for n to some space or tab(t)
sed ‘N;s/\n/ /’ hello.txt


Example 12. To give numbering to all the lines after search and replace (Use = option)
sed = hello.txt
Output
1
shaan car
2
sonali bike

Also use N option to combine two lines
sed = hello.txt | sed ‘N;s/\n/\t/’
Output
1   shaan car
2   sonali bike

Example 13. Finds one word, replace another word with any third word.
To search "shaan" and in the searched line, replace "abc" with "xyz" 
sed ‘/shaan/ s/abc/xyz/’ hello.txt

Search for "car" from first line to the search term "shaan" and replace "abc" with "xyz"
sed ‘1,/shaan/ s/abc/xyz/’ hello.txt

Search from 3rd line to till it find "shaan"
sed ‘3,/shaan/ s/abc/xyz/’ hello.txt

Search and replace "abc" with "xyz" between two search terms "shaan" and "sonali"
sed ‘/shaan/,/sonali/ s/abc/xyz/’ hello.txt

Searching from a search pattern to end of the line
sed ‘/shaan/,$ s/abc/xyz/’ hello.txt


Example 14. Using negation operation ( ! )
Display all the lines which do not contain "abc" word
sed –n ‘/abc/ !p’ hello.txt

Delete all lines except the lines which contain "shaan"
sed '/shaan/ !d' hello.txt

You can combine all the above mention techniques with this negation option.
Delete all the lines except 1 to 3
sed '1,3 !d'

 
Example 15. Using scripting operator (-f switch)
It is useful for writing complex SED script.
SED is one more scripting language such as Shell scripting, AWK and Perl etc.
sed -f mySedScript.sed

No comments:

Post a Comment

Note: only a member of this blog may post a comment.