This blog talks about the most usable “sed” commands in a day-to-day life to reduce coding time.
Nowadays, in IT industry many tasks are common and most of
us are doing some repetitive jobs which consumes a lot of time. Some tasks are really very boring
like “deleting words” or “inserting or replacing some text” in the file.
For one or two file, it is easy to do modification manually
but when there is a number of files required same modification then we
generally use “sed” and “awk” command which makes our life easier.
Following are the 10 most usable sed commands:
1. For doing search and replace in the file
globally:
- sed –i ‘s/OLD_PATTERN/NEW_PATTERN/g’ filename
Example: sed –i ‘s/Hi/Hello/g’ myfile
Data in myfile before sed command:
Hi
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
Data in myfile after sed command:
Hello
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
2. Appending any line after specific pattern matched in the file:
- sed -i "/PATTERN/a\ LINE_TO_BE_ADDED" filename
Example: sed –i “Hello/a\ I am Munjal” myfile
Data in myfile after sed command:
Hello
I am Munjal
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
3. Deleting any line which contain specific pattern:
- sed –i ‘/PATTERN/d’ filename
Example: sed –i ‘/Munjal/d’ myfile
Data in myfile after sed command:
Hello
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
4. Deleting all lines from starting match to ending match:
- sed -i '/STARTING_MATCH_PATTERN/,/ENDING_MATCH_PATTERN/d' filename
Example: sed –i ‘s/How are you/,/Have you take lunch/d’ myfile
Data in myfile after sed command:
Hello
I am going to mall.
5. Case insensitive search and replace:
- sed -i 's/OLD_PATTERN/NEW_PATTERN/I' filename
Example: sed –i ‘/hello/Hi/I’ myfile
Data in myfile after sed command:
Hi
I am going to mall.
6. Inserting a blank line above any pattern:
- sed -i ‘s/PATTERN/\n&/g’ filename
Example: sed –i ‘/going/\n&/g’ myfile
Data in myfile after sed command:
Hi
I am going to mall.
7. Deleting multiple lines after pattern match with matching line:
- sed -i '/PATTERN/,+Nd' filename
Example: sed –i ‘/How are you/,+2d’ myfile
Data in myfile before sed command:
Hello
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
Data in myfile after sed command:
Hello
I am going to mall.
8. Inserting multiple lines in the file from another file:
- sed -i '/ PATTERN/r filename_to_add_lines' filename
Example: sed –i ‘/Are you busy/r yourfile’ myfile
Data in myfile before sed command:
Hello
How are you?
Are you busy?
Have you take lunch?
I am going to mall.
Data in “yourfile”:
I am not busy.
I just wake up.
Data in myfile after sed command:
Hello
How are you?
Are you busy?
I am not busy.
I just wake up.
Have you take lunch?
I am going to mall.
9. Converting multiple blank line to single blank line:
- sed –i ‘/^$/N;/^\n$/D’ filename
By using above sed command, it replaces multiple consecutive blank lines with single blank line which looks better during reviewing the code.
10. Changing the pattern by grouping words:
- sed –i ‘s/\(.*\)PATTERN\(.*\)/\2PATTERN\1/g’ filename
Example: sed –i ‘s/\(.*\) are \(.*\)/\2 are \1/g’ myfile
Data in myfile after sed command:
Hello
you? are How
Are you busy?
I am not busy.
I just wake up.
Have you take lunch?
I am going to mall.
11. Adding a new line before pattern match
11. Adding a new line before pattern match
- sed -i '/PATTERN/i LINE' filename
12. Adding a new line after pattern match
- sed -i '/PATTERN/a LINE' filename
Reference:
(1) http://www.grymoire.com/Unix/Sed.html
(2) http://www.computerhope.com/unix/used.htm