Ade Malsasa Akbar contact
Senior author, Open Source enthusiast.
Tuesday, August 9, 2016 at 13:32


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)

  1. echo “UNIX UNIX UNIX” | sed ‘s/UNIX/GNU/g’
  2. echo “UNIX UNIX UNIX” | sed ‘s/UNIX/GNU/’
4. Find & Replace (Uppercase)
  1. sed ‘s/have/\U&/g’ text.txt
5. Find & Replace (Lowercase)
  1. sed ‘s/UNIX/\L&/g’ text.txt
  2. sed ‘s/GNU/\L&/g’ text.txt
  3. echo “UNIX MINIX” | sed ‘s/MINIX/\L&/g’
6. Delete All Lines
  1. sed ‘d’ text.txt
  2. sed -i ‘d’ text.txt
  3. sed ‘d’ text.txt > new_text.txt
7. Delete Particular Line
  1. sed ‘1d’ text.txt
  2. sed ‘1,3d’ text.txt
  3. sed ‘4,6d’ text.txt
8. Delete All Blank Lines
sed ‘/^$/d’ text.txt

9. Reverse Delete Blank Lines
sed ‘/^$/!d’ text.txt

10. Delete Lines Containing Particular String
sed ‘/UNIX/d’ text.txt


Part 2


11. Find & Replace (Specific Line)
  1. sed '3 s/bsd/\U&/g' text2.txt
  2. sed '1 s/gnu/\U&/g' text2.txt
  3. 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)
  1. sed ‘1 s/gnu/\U&/1’ text2.txt
  2. 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)
  1. sed 's/\(bsd\)/\1 \1 \1/g' text2.txt
  2. sed 's/\(gnu\)/\1 \1 \1/g' text2.txt
16. Find & Replace (Decorate Column)
  1. sed 's/\(bsd\)/[\1]/g' text2.txt
  2. 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)
  1. sed '/unix/Id' text5.txt
  2. sed '/bsd/Id' text5.txt
  3. sed '/gnu/Id' text5.txt
20. Delete Line (OR Logic Regex)
  1. sed '/<-\|->/d' text4.txt
  2. sed '/sign\|just/d' text4.txt
  3. 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)
  1. for i in *.txt; do mv "$i" "`echo $i | sed "s/text/flext/g"`"; done
  2. for i in *.txt; do mv -v "$i" "`echo $i | sed "s/^[0-9]*[0-9]//g"`"; done

26. Delete Multiple Files (bash Looping)
  1. for i in *.txt; do rm -v "`echo $i | sed "s/^[0-9]*[0-9]//g"`"; done
  2. for i in *.txt; do rm -v "`echo $i | sed -n "/^[0-9]*[0-9]/p"`"; done

27. Change Slash Delimiters to Another Characters
  1. sed 's/unix/CHANGED/g' text6.txt
  2. sed 's@unix@CHANGED@g' text6.txt
  3. sed 's!unix!CHANGED!g' text6.txt
  4. 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)
  1. for i in *.txt; do sed ‘q’ $i; done
  2. for i in *.txt; do sed -n '$ F' $i; sed 'q' $i; done
39. Print Multiple Files' Last Lines (bash Looping)
  1. for i in *.txt; do sed -n '$ p' $i; done
  2. 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
  1. sed -n '/this.*comment/p' text14.txt
  2. 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
  1. sed '1i # THIS IS A NEW LINE' text13.txt
  2. 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
  1. sed -n '/gnu/,/dunix/p' text15.txt
  2. sed -n '/dunix/,/sunos/Ip' text15.txt
52. Delete Only Comment Lines Between Address Range
  1. sed '10,19{/\/\*/,/\*\//d}' text14.txt
  2. sed '10,19{/\/\//d}' text14.txt
53. Delete Only Comment Lines Between Two Patterns
  1. sed '/printf/,/printf/{/\/\*/,/\*\//d}' text14.txt
  2. 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