SED Command

SED or stream editor is a text editor for modifying UNIX text files. It edits files line by line in a non-interactive way. SED command can be used for replacing, deleting, printing the matching pattern in a file. SED does not originally modify the content of file but it will display the modified output. We can store this output in another file by output redirection. As SED displays the edited output of file without opening it, it is a much quicker way of finding and replacing.

Examples of SED Command

Delete first line or header line of the file: Sed ‘1d’ filename
Cat filename
Abcd pqrs
Mno
Hjk

Sed ‘1d’ filename
Mno
Hjk

Delete last line of the file:
sed ‘$d’ filename

Delete from 3rd to 9th line:
sed ‘3,9 d’ filename

Delete from 4th to last line:
sed ‘4, $ d’ filename

To delete blank lines from file:
sed ‘/^$/d’ filename

Delete the line containing the string “aks”:
sed ‘/aks/d’ filename

To make the changes permanent in source file itself, use option “-i”:
sed -i ’$d’ filename

To delete line other than the one specified:
sed ‘ 1, 6!d’ file

To delete 1st and 3rd line of a file:
sed ‘1d;3d’ filename



To delete all lines ending with either ‘a’ or ‘A’ i.e., case insensitive delete:
sed ‘[aA]$/d’ filename

To delete all lines which are entirely in capital letters:
sed ‘/^[A-Z]*$/d’ filename

To delete line containing the pattern ‘abc’ or ‘xyz’:
sed ‘/abc\|xyz/d’ filename

To delete lines starting from 2nd line till the line where the pattern ‘abc’ is found:
sed ‘2,/abc/d’ filename

To delete the last line only if contains the pattern ‘xyz’:
sed ‘${/abc/d;}’ filename

To delete the last line if it contains either ‘xyz’ or ‘abc’:
sed ‘${/xyz\|abc/d;}’ filename

To delete lines having pattern ‘abc’ and also delete next line:
sed ‘/abc/{N;d;}’ filename

To delete the line containing the pattern ‘abc’ only it is found in the range of 2nd to 7th line:
sed ‘2,7{/abc/d;}’ filename

To replace first occurrence of the search string. For e.g., find first ‘abcd’ and replace it with ‘pqrs’
Sed ‘s/abcd/pqrs/’ filename

To replace nth occurrence of the search string use /n where n is 1,2, 3.. etc. For example to replace 3rd occurrence of abcd with pqrs
Sed ‘s/abcd/pqrs/2’ filename

To replace all the occurrence of the string in file use ‘g’. For example, to replace all occurrence of abcd with pqrs
Sed ‘s/abcd/pqrs/g’ filename




To replace from nth occurrence till the end of the file use ‘ng’. For e.g., to replace all occurrence of abcd from 4th row till end with pqrs
Sed ‘s/abcd/pqrs/4g’ filename

Regular Expressions

Regular expressions are used to search patterns in text file. Below is a list of special characters that are used in regular expression for search strings
Character Description
^ –Matches beginning of the line
$ –Matches end of the line
. –Matches single character
* –Matches any occurrences of the previous character
[ ] –Matches all the characters in the [ ]

Regular expression Description
/./ –Matches line containing at least one character
/../ –Matches line containing at least two characters
/^#/ –Matches line beginning with a ‘#’
/^$/ –Matches all blank lines
/}$/ –Matches lines ending with ‘}’ [no spaces]
/} *$/ –Matches any line ending with ‘}’ followed by zero or more spaces
/[xyz]/ –Matches any line that contains a lowercase ‘x’, ‘y’, or ‘z’
/^[xyz]/ –Matches any line that begins with an ‘x’, ‘y’, or ‘z’