This article is a long compilation of our six series of GNU sed command line examples. This article contains more than 60 command lines divided in 6 part. This is intended for those who want to practice, hence this article contains no explanation for every example. We hope this helps you to learn GNU sed easier.
GNU sed Examples The Series
Part 1
1. Read Text File
sed r text.txt
2. Find &
Replace
sed ‘s/UNIX/MINIX/g’
text.txt
3. Find &
Replace ('g' Flag)
-
echo “UNIX UNIX UNIX” | sed ‘s/UNIX/GNU/g’
-
echo “UNIX UNIX UNIX” | sed ‘s/UNIX/GNU/’
-
sed ‘s/have/\U&/g’ text.txt
-
sed ‘s/UNIX/\L&/g’ text.txt
-
sed ‘s/GNU/\L&/g’ text.txt
-
echo “UNIX MINIX” | sed ‘s/MINIX/\L&/g’
-
sed ‘d’ text.txt
-
sed -i ‘d’ text.txt
-
sed ‘d’ text.txt > new_text.txt
-
sed ‘1d’ text.txt
-
sed ‘1,3d’ text.txt
-
sed ‘4,6d’ text.txt
sed ‘/^$/d’ text.txt
sed ‘/^$/!d’ text.txt
sed ‘/UNIX/d’ text.txt
Part 2
11. Find & Replace (Specific Line)
-
sed '3 s/bsd/\U&/g' text2.txt
-
sed '1 s/gnu/\U&/g' text2.txt
-
sed '2 s/unix/\U&/g' text2.txt
12. Find &
Replace (Column Number)
sed ‘s/gnu/\U&/1’ text2.txt
13. Find &
Replace (Column Number & Specific Line)
-
sed ‘1 s/gnu/\U&/1’ text2.txt
-
sed ‘2 s/unix/\U&/1’ text2.txt
14. Find &
Replace (Column Number & 'g' Flag)
sed ‘1 s/gnu/\U&/g1’ text2.txt
15. Find &
Replace (Duplicate Column)
-
sed 's/\(bsd\)/\1 \1 \1/g' text2.txt
-
sed 's/\(gnu\)/\1 \1 \1/g' text2.txt
16. Find &
Replace (Decorate Column)
-
sed 's/\(bsd\)/[\1]/g' text2.txt
-
sed 's/\(gnu\)/{\1}/g' text2.txt
17. Find &
Replace (Delete All Line Numbers)
sed 's/^[0-9]*[0-9].//g' text3.txt
18. Find &
Replace (Case Insensitive)
sed 's/bsd/changed/Ig' text2.txt
19. Delete Line
(Case Insensitive)
-
sed '/unix/Id' text5.txt
-
sed '/bsd/Id' text5.txt
-
sed '/gnu/Id' text5.txt
20. Delete Line
(OR Logic Regex)
-
sed '/<-\|->/d' text4.txt
-
sed '/sign\|just/d' text4.txt
-
sed '/sign\|just\|in/d' text4.txt
Part 3
21. Run Multiple
Commands
sed -e 's/gnu/\U&/g' -e 's/bsd/\U&/g' text6.txt
22. Run Multiple
Commands (Simpler)
sed 's/gnu/\U&/g; s/bsd/\U&/g' text6.txt
23. Combine sed
with bash Looping Commands
for i in {6..8}; do sed '/bsd/d' "text$i.txt"; echo "";
done;
24. Delete
Contents of Multiple Files (bash Looping)
for i in {6..8}; do sed -i 'd' text$i.txt; done
25. Rename
Multiple Files (bash Looping)
-
for i in *.txt; do mv "$i" "`echo $i | sed "s/text/flext/g"`"; done
-
for i in *.txt; do mv -v "$i" "`echo $i | sed "s/^[0-9]*[0-9]//g"`"; done
26. Delete
Multiple Files (bash Looping)
-
for i in *.txt; do rm -v "`echo $i | sed "s/^[0-9]*[0-9]//g"`"; done
-
for i in *.txt; do rm -v "`echo $i | sed -n "/^[0-9]*[0-9]/p"`"; done
27. Change Slash
Delimiters to Another Characters
-
sed 's/unix/CHANGED/g' text6.txt
-
sed 's@unix@CHANGED@g' text6.txt
-
sed 's!unix!CHANGED!g' text6.txt
-
sed 's?unix?CHANGED?g' text6.txt
28. Adjust Line
Spacing (Single)
sed ‘G’ text6.txt
29. Adjust Line
Spacing (Double)
sed 'G;G' text6.txt
30. Find &
Replace (Lines Range)
sed '1,3 s/unix/CHANGED/g' text6.txt
Part 4
31. Print Matched
Lines
sed -n '/unix/p' text9.txt
32. Print
Reverse-Matched Lines
sed -n '/unix/!p' text9.txt
33. Bash Looping
with Delay Time
for i in *.txt; do sed 's/x/[X]/g' $i; echo " "; sleep 1;
done
34. Print The
File Name
sed -n '$ F' text9.txt
35. Print The
First Line
sed ‘q’ text9.txt
36. Print The
Last Line
sed -n '$ p'
text9.txt
37. Count Total
Line Number
sed -n '$ =' text9.txt
38. Print
Multiple Files' First Lines (bash Looping)
-
for i in *.txt; do sed ‘q’ $i; done
-
for i in *.txt; do sed -n '$ F' $i; sed 'q' $i; done
39. Print
Multiple Files' Last Lines (bash Looping)
-
for i in *.txt; do sed -n '$ p' $i; done
-
for i in *.txt; do sed -n '$ F' $i; sed -n '$ p' $i; done
40. Count
Multiple Files Line Numbers (bash Looping)
for i in *.txt; do sed -n '$ F; $ =' "$i"; done
Part 5
41. Delete
Comment Lines (Double Slash Style)
sed '/^\/\/.*$/d' text12.txt
42. Delete
Comment Lines (Double Slash Style, Except At Line Beginning)
sed 's+\([^!]\/\/.*$\)++g' text12.txt
43. Delete
Comment Lines (Hash Style)
sed '/^#[^!].*.$/d' text13.txt
44. Delete
Comment Lines (Slash-Asterisk Style)
sed '/\/\*/,/\*\//d' text14.txt
45. Boolean
Operator OR
sed -E -n '/this|line|return/p' text14.txt
46. Boolean
Operator AND
-
sed -n '/this.*comment/p' text14.txt
-
sed -n '/this.*slash.*comment.*/p' text14.txt
47. Multiple
Convert Images (PNG to JPEG)
for i in *.png; do convert -verbose "$i" "`echo $i |
sed 's/.png/.jpeg/g'`"; done
48. Multiple
Convert Images (JPEG to PNG)
for i in *.jpeg; do convert -verbose "$i" "`echo $i |
sed 's/.jpeg/.png/g'`"; done
49. Insert A
Single Line
-
sed '1i # THIS IS A NEW LINE' text13.txt
-
sed '3i # THIS IS A NEW LINE' text13.txt
50. Insert
Multiple Lines
sed '3i # THIS IS A NEW LINE\n# THIS IS ANOTHER LINE' text13.txt
Part 6
51. Print Only
Lines Between Two Patterns
-
sed -n '/gnu/,/dunix/p' text15.txt
-
sed -n '/dunix/,/sunos/Ip' text15.txt
52. Delete Only Comment Lines Between Address
Range
-
sed '10,19{/\/\*/,/\*\//d}' text14.txt
-
sed '10,19{/\/\//d}' text14.txt
53. Delete Only
Comment Lines Between Two Patterns
-
sed '/printf/,/printf/{/\/\*/,/\*\//d}' text14.txt
-
sed '/^/,/main/{/\/\*/,/\*\//d}' text14.txt
54. Edit Only
Matched Lines Between Address Range
sed '4,7{s/x/[X]/Ig}' text15.txt
55. Edit All
Uppercase Characters (POSIX Character Class)
sed 's/[[:upper:]]/[X]/g' text15.txt
56. Edit All
Lowercase Characters (POSIX Character Class)
sed 's/[[:lower:]]/[X]/g' text15.txt
57. Edit All
Punctuation Characters (POSIX Character Class)
sed 's/[[:punct:]]/[X]/g' text15.txt
58. Edit All
Uppercase & Lowercase (POSIX Character Class)
sed 's/[[:alpha:]]/[X]/g' text15.txt
59. Edit All
Numeric Characters (POSIX Character Class)
sed 's/[[:digit:]]/[X]/g' text15.txt
60. Edit All
Space & Tab Characters (POSIX Character Class)
sed 's/[[:blank:]]/[X]/g' text15.txt