SED stands for Stream EDitor. It was developed in 1974 by Lee E. McMahon of Bell Labs. It is most widely used today in the industry. It process line by line. It can be used to do various kinds of operations on the text file. Lets start learning from its basics.
Deleting N th line
sed 3d abc.txt
It will delete the 3rd line from the file abc.txt
Deleting alternate lines
sed '1~2d' abc.txt
sed '2~2d' abc.txt
Use the first sed command to delete odd lines and the second one to delete even lines in the file abc.txt.
Delete from 5th to 11th line in a file
sed '5,11d' abc.txt
It deletes 5th line to 11th line from the file abc.txt
Delete the last line from a file
sed '$d' abc.txt
It deletes the last line in abc.txt file
Delete the line that matches the given pattern
sed '/unix/d' abc.txt
It will delete the line that contains the word unix in it.
Delete the lines from matched pattern to its next 3 lines
sed '/unix/,+3d' abc.txt
It will delete 3 lines from the line that contains the word unix in abc.txt file
Delete the lines from matched pattern to end of file
sed '/unix/,$d' abc.txt
It will delete till the end of file abc.txt from the line that contains unix word in it.
Delete all the blank lines in file
sed '/^$/d' abc.txt
It will remove all the blank lines in a file abc.txt
Delete all the commented lines in file
sed '/#.*/d' abc.txt
It will delete all the commented lines from the file abc.txt, comments are denoted by symbol #.
s stands for substitution, / acts as delimiter, lets learn various kinds of substitutions
Replace old with new
sed 's/old/new/'
This will replace the word old with new
Replace old with new globally
sed 's/old/new/g'
This will replace all the occurance of the word old with new
Substitution commands can be used in pipeline
echo "old" | sed 's/old/new/'
The word new will get displayed on the console
Substitute the word old with new in a file
sed 's/old/new/' filename
This will replace word old with new and display the replaced word on the console
Substitute the word old with new in a file and save it
sed -i 's/old/new/' filename
The replaced file gets saved with the -i option
Deleting N th line
sed 3d abc.txt
It will delete the 3rd line from the file abc.txt
Deleting alternate lines
sed '1~2d' abc.txt
sed '2~2d' abc.txt
Use the first sed command to delete odd lines and the second one to delete even lines in the file abc.txt.
Delete from 5th to 11th line in a file
sed '5,11d' abc.txt
It deletes 5th line to 11th line from the file abc.txt
Delete the last line from a file
sed '$d' abc.txt
It deletes the last line in abc.txt file
Delete the line that matches the given pattern
sed '/unix/d' abc.txt
It will delete the line that contains the word unix in it.
Delete the lines from matched pattern to its next 3 lines
sed '/unix/,+3d' abc.txt
It will delete 3 lines from the line that contains the word unix in abc.txt file
Delete the lines from matched pattern to end of file
sed '/unix/,$d' abc.txt
It will delete till the end of file abc.txt from the line that contains unix word in it.
Delete all the blank lines in file
sed '/^$/d' abc.txt
It will remove all the blank lines in a file abc.txt
Delete all the commented lines in file
sed '/#.*/d' abc.txt
It will delete all the commented lines from the file abc.txt, comments are denoted by symbol #.
s stands for substitution, / acts as delimiter, lets learn various kinds of substitutions
Replace old with new
sed 's/old/new/'
This will replace the word old with new
Replace old with new globally
sed 's/old/new/g'
This will replace all the occurance of the word old with new
Substitution commands can be used in pipeline
echo "old" | sed 's/old/new/'
The word new will get displayed on the console
Substitute the word old with new in a file
sed 's/old/new/' filename
This will replace word old with new and display the replaced word on the console
Substitute the word old with new in a file and save it
sed -i 's/old/new/' filename
The replaced file gets saved with the -i option