Let us consider a file with the sample contents as below:
# cat file Cygwin Unix Linux Solaris AIX
1. Delete the 1st line or the header line:
# sed '1d' file Unix Linux Solaris AIX
d command is to delete a line. 1d means to delete the first line.
The above command will show the file content by deleting the first line. However, the source file remains unchanged. To update the original file itself with this deletion or to make the changes permanently in the source file, use the -i option. The same is applicable for all the other examples.
# sed -i '1d' file
Note: -i option in sed is available only if it is GNU sed. If not GNU, re-direct the sed output to a file, and rename the output file to the original file.
2. Delete a particular line, 3rd line in this case:
# sed '3d' file Cygwin Unix Solaris AIX
3. Delete the last line or the trailer line of the file:
# sed '$d' file Cygwin Unix Linux Solaris
$ indicates the last line.
4. Delete a range of lines, from 2nd line till 4th line:
# sed '2,4d' file Cygwin AIX
The range is specified using the comma operator.
5. Delete lines other than the specified range, line other than 2nd till 4th here:
# sed '2,4!d' file Unix Linux Solaris
The ! operator indicates negative condition.
6. Delete the first line AND the last line of a file, i.e, the header and trailer line of a file.
# sed '1d;$d' file Unix Linux Solaris
Multiple conditions are separated using the ‘;’ operator. Similarly, say to delete 2nd and 4th line, you can use: ‘2d;3d’.
7. Delete all lines beginning with a particular character, ‘L’ in this case:
# sed '/^L/d' file Cygwin Unix Solaris AIX
‘^L’ indicates lines beginning with L.
8. Delete all lines ending with a particular character, ‘x’ in this case:
# sed '/x$/d' file Cygwin Solaris AIX
‘x$’ indicates lines ending with ‘x’. AIX did not get deleted because the X is capital.
9. Delete all lines ending with either x or X, i.e case-insensitive delete:
# sed '/[xX]$/d' file Cygwin Solaris
[xX] indicates either ‘x’ or ‘X’. So, this will delete all lines ending with either small ‘x’ or capital ‘X’.
10. Delete all blank lines in the file
# sed '/^$/d' file Cygwin Unix Linux Solaris AIX
‘^$’ indicates lines containing nothing and hence the empty lines get deleted. However, this wont delete lines containing only some blank spaces.
11. Delete all lines which are empty or which contains just some blank spaces:
# sed '/^ *$/d' file Cygwin Unix Linux Solaris AIX
‘*’ indicates 0 or more occurrences of the previous character. ‘^ *$’ indicates a line containing zero or more spaces. Hence, this will delete all lines which are either empty or lines with only some blank spaces.
12. Delete all lines which are entirely in capital letters:
# sed '/^[A-Z]*$/d' file Cygwin Unix Linux Solaris
[A-Z] indicates any character matching the alphabets in capital.
13. Delete the lines containing the pattern ‘Unix’.
# sed '/Unix/d' file Cygwin Linux Solaris AIX
The pattern is specified within a pair of slashes.
14. Delete the lines NOT containing the pattern ‘Unix’:
# sed '/Unix/!d' file Unix
15. Delete the lines containing the pattern ‘Unix’ OR ‘Linux’:
# sed '/Unix\|Linux/d' file Cygwin Solaris AIX
The OR condition is specified using the | operator. In order not to get the pipe(|) interpreted as a literal, it is escaped using a backslash.
16. Delete the lines starting from the 1st line till encountering the pattern ‘Linux’:
# sed '1,/Linux/d' file Solaris AIX
Earlier, we saw how to delete a range of lines. Range can be in many combinations: Line ranges, pattern ranges, line and pattern, pattern and line.
17. Delete the lines starting from the pattern ‘Linux’ till the last line:
$ sed '/Linux/,$d' file
Cygwin
Unix
18. Delete the last line ONLY if it contains the pattern ‘AIX’:
# sed '${/AIX/d;}' file Cygwin Unix Linux Solaris
$ is for the last line. To delete a particular line only if it contains the pattern AIX, put the line number in place of the $. This is how we can implement the ‘if’ condition in sed.
19. Delete the last line ONLY if it contains either the pattern ‘AIX’ or ‘HPUX’:
# sed '${/AIX\|HPUX/d;}' file Cygwin Unix Linux Solaris
20. Delete the lines containing the pattern ‘Solaris’ only if it is present in the lines from 1 to 4.
# sed '1,4{/Solaris/d;}' file Cygwin Unix Linux AIX
This will only delete the lines containing the pattern Solaris only if it is in the 1st four lines, nowhere else.
21. Delete the line containing the pattern ‘Unix’ and also the next line:
# sed '/Unix/{N;d;}' file Cygwin Solaris AIX
N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line.
22. Delete only the next line containing the pattern ‘Unix’, not the very line:
# sed '/Unix/{N;s/\n.*//;}' file Cygwin Unix Solaris AIX
Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.
23. Delete the line containing the pattern ‘Linux’, also the line before the pattern:
# sed -n '/Linux/{s/.*//;x;d;};x;p;${x;p;}' file | sed '/^$/d' Cygwin Solaris AIX
The second part of sed is to remove the empty lines created by the first sed command.
24. Delete only the line prior to the line containing the pattern ‘Linux’, not the very line:
# sed -n '/Linux/{x;d;};1h;1!{x;p;};${x;p;}' file Cygwin Linux Solaris AIX
25. Delete the line containing the pattern ‘Linux’, the line before, the line after:
# sed -n '/Linux/{N;s/.*//;x;d;};x;p;${x;p;}' file | sed '/^$/d' Cygwin AIX
26. To remove a specific character, say ‘a’
$ sed 's/a//' file Linux Solris Ubuntu Fedor RedHt
This will remove the first occurence of ‘a’ in every line of the file. To remove all occurences of ‘a’ in every line,
$ sed 's/a//g' file
27. To remove 1st character in every line:
$ sed 's/^.//' file inux olaris buntu edora edHat
.(dot) tries to match a single character. The ^ tries to match a pattern(any character) in the beginning of the line. Another way to write the same:
$ sed 's/.//' file
This tells to replace a character with nothing. Since by default, sed starts from beginning, it replaces only the 1st character since ‘g’ is not passed.
28. To remove last character of every line :
$ sed 's/.$//' file Linu Solari Ubunt Fedor RedHa
The $ tries to match a pattern in the end of the line.
29. To remove the 1st and last character of every line in the same command:
$ sed 's/.//;s/.$//' file inu olari bunt edor edHa
Two commands can be given together with a semi-colon separated in between.
30. To remove first character only if it is a specific character:
$ sed 's/^F//' file Linux Solaris Ubuntu edora RedHat
This removes the 1st character only if it is ‘F’.
31. To remove last character only if it is a specific character:
$ sed 's/x$//' file Linu Solaris Ubuntu Fedora RedHat
This removed the last character only if it s ‘x’.
32. To remove 1st 3 characters of every line:
$ sed 's/...//' file ux aris ntu ora Hat
A single dot(.) removes 1st character, 3 dots remove 1st three characters.
33. To remove 1st n characters of every line:
$ sed -r 's/.{4}//' file x ris tu ra at
.{n} -> matches any character n times, and hence the above expression matches 4 characters and deletes it.
34. To remove last n characters of every line:
$ sed -r 's/.{3}$//' file Li Sola Ubu Fed Red
35. To remove everything except the 1st n characters in every line:
$ sed -r 's/(.{3}).*/\1/' file Lin Sol Ubu Fed Red
.* -> matches any number of characters, and the first 3 characters matched are grouped using parantheses. In the replacement, by having \1 only the group is retained, leaving out the remaining part.
36. To remove everything except the last n characters in a file:
$ sed -r 's/.*(.{3})/\1/' file nux ris ntu ora Hat
Same as last example, except that from the end.
37. To remove multiple characters present in a file:
$ sed 's/[aoe]//g' file Linux Slris Ubuntu Fdr RdHt
To delete multiple characters, [] is used by specifying the characters to be removed. This will remove all occurences of the characters a, o and e.
38. To remove a pattern :
$ sed 's/lari//g' file Linux Sos Ubuntu Fedora RedHat
Not just a character, even a pattern can be removed. Here, ‘lari’ got removed from ‘Solaris’.
39. To delete only nth occurrence of a character in every line:
$ sed 's/u//2' file Linux Solaris Ubunt Fedora RedHat
By default, sed performs an activity only on the 1st occurence. If n is specifed, sed performs only on the nth occurence of the pattern. The 2nd ‘u’ of ‘Ubuntu’ got deleted.
40. To delete everything in a line followed by a character:
$ sed 's/a.*//' file Linux Sol Ubuntu Fedor RedH
41. To remove all digits present in every line of a file:
$ sed 's/[0-9]//g' file
[0-9] stands for all characters between 0 to 9 meaning all digits, and hence all digits get removed.
42. To remove all lower case alphabets present in every line:
$ sed 's/[a-z]//g' file L S U F RH
[a-z] represents lower case alphabets range and hence all lower-case characters get removed.
43. To remove everything other than the lower case alphabets:
$ sed 's/[^a-z]//g' file inux olaris buntu edora edat
^ inside square brackets negates the condition. Here, all characters except lower case alphabets get removed.
44. To remove all alpha-numeric characters present in every line:
$ sed 's/[a-zA-Z0-9]//g' file
All alpha-numeric characters get removed.
45. To remove a character irrespective of the case:
$ sed 's/[uU]//g' file Linx Solaris bnt Fedora RedHat