sed examples

 

1. Replace every occurrence of Windows with Linux in myFile.txt

  • sed 's/Windows/Linux/g' myFile.txt

 

2. Replace every occurrence of Windows with Linux in myFile.txt and send to newFile.txt

  • sed 's/Windows/Linux/g' myFile.txt > newFile.txt
  • or
  • cat myFile.txt | sed 's/Windows/Linux/g' > newFile.txt

 

3. Replace every occurrence of Windows or windows with Linux.

  • sed 's/Windows|windows/Linux/g' myFile.txt

 

4. Add 8 spaces to the left of a text for pretty printing.

  • sed 's/^/ /' myFile.txt > newFile.txt

 

5. Display only one paragraph, starting with "Dear Sir" and ending in "Kind regards"

  • sed -n '/Dear Sir/,/Kind regards/p'

 

6. Show only lines 13-17 of myFile.txt

  • sed -n 13,17p myFile.txt

 

7. Show all of myFile.txt except for lines from 17 to 22

  • sed 17,22d myFile.txt

 

8. Double-space myFile.txt

  • sed G myFile.txt

 

9. Write all commands in script.sed and execute them

  • sed -f script.sed myFile.txt

 

10. Replace Windows with Linux in myFile.txt except in the 6th line

  • sed '6!s/Windows/Linux/' myFile.txt

 

11. Delete the last line

  • sed '$d' myFile.txt

 

12. Print only lines with five consecutive digits

  • sed '/[0-9]\{5\}/p' myFile.txt

 

13. Unless Ubuntu is found replace Windows with Linux

  • sed '/Ubuntu/!s/Windows/Linux/' myFile.txt

 

14. Delete all lines from line 11 to 'Linux'

  • sed '11,/Linux/d' myFile.txt

 

15. Replace one with unos in a case-insensitive manner, so it will print "unos TWO"

  • echo ONE TWO | sed "s/one/unos/I"

 

16. Triple-space a file

  • sed 'G;G' myFile.txt

 

17. Alternative to dos2unix

  • sed 's/.$//' myFile.txt

 

18. Delete all spaces in front of every line of myFile.txt

  • sed 's/^[ ^t]*//' myFile.txt

 

19. Delete all spaces at the end of every line of myFile.txt

  • sed 's/[ ^t]*$//' myFile.txt

 

20. Delete all spaces in front and at the end of every line of myFile.txt

  • sed 's/^[ ^t]*//;s/[ ^]*$//' myFile.txt

 

21. Replace windows with linux only for the first instance in a line.

  • sed 's/windows/linux/' myFile.txt

 

22. Replace windows with linux only for the 4th instance in a line.

  • sed 's/windows/linux/4' myFile.txt

 

23. Replace windows with linux for all instances in a line.

  • sed 's/windows/linux/g' myFile.txt

 

24. Only if line contains Ubuntu, substitute Windows with Linux

  • sed '/Ubuntu/s/Windows/Linux/g' myFile.txt

 

25. Delete all consecutive blank lines except for EOF

  • sed '/./,/^$/!d' myFile.txt

 

26. Delete all consecutive blank lines, but allows only top blank line

  • sed '/^$/N;/\n$/D' myFile.txt

 

27. Delete all leading blank lines

  • sed '/./,$!d' myFile.txt

 

28. Delete all trailing blank lines

  • sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' myFile.txt

 

29. If a file ends in a backslash, join it with the next (useful for shell scripts)

  • sed -e :a -e '/\$/N; s/\\n//; ta' myFile.txt

 

30. Match regex plus the next 7 lines

  • sed '/regex/,+7/expr/'

 

31. Delete every fifth line, starting with the first

  • sed '1~5d' myFile.txt

 

32. Print every 7th line starting with the third

  • sed -n '3~7p' myFile.txt

 

33. Another way to write example 15 above.

  • sed 's/[Nn]ick/Linux/g' myFile.txt

 

34. Print only the first match of RE (regular expression)

  • sed -n '/RE/{p;q;}' myFile.txt

 

35. Delete only the first match

  • sed '0,/RE/{//d;}' myFile.txt

 

36. Change only the first match

  • sed '0,/RE/s//to_that/' myFile.txt

 

37. Change first field to 9999 in a CSV file

  • sed 's/^[^,]*,/9999,/' file.csv

 

38. sed script to convert CSV file to bar-separated (works only on some types of CSV, with embedded "s and commas)

  • s/^ *\(.*[^ ]\) *$/||/;
    s/" *, */"|/g;
    : loop
    s/| *\([^",|][^,|]*\) *, */||/g;
    s/| *, */||/g;
    t loop
    s/ *|/|/g;
    s/| */|/g;
    s/^|\(.*\)|$//;

 

39. Change numbers from myFile.txt from 1234.56 form to 1.234.56

  • sed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/,/g;ta' myFile.txt

 

40. Convert any word starting with reg or exp to uppercase

  • sed -r "s/\<(reg|exp)[a-z]+/\U&/g"

 

41. Do replacement of Windows with Linux only on lines between 1 and 20

  • sed '1,20 s/Windows/Linux/g' myFile.txt

 

42. The above reversed (match all except lines 1-20)

  • sed '1,20 !s/Windows/Linux/g' myFile.txt

 

43. Replace only between "from" and "until"

  • sed '/from/,/until/ { s/\<red\>/magenta/g; s/\<blue\>/cyan/g; }' myFile.txt

 

44. Replace only from the word "ENDNOTES:" until EOF

  • sed '/ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; }' myFile.txt

 

45. Print paragraphs only if they contain regex

  • sed '/./{H;$!d;};x;/regex/!d' myFile.txt

 

46. Print paragraphs only if they contain RE1, RE2 and RE3

  • sed -e '/./{H;$!d;}' -e 'x;/RE1/!d;/RE2/!d;/RE3/!d' myFile.txt

 

47. Join two lines in the first ends in a backslash

  • sed ':a; /\$/N; s/\\n//; ta' myFile.txt

 

48. This is how you can use double quotes

  • sed 's/14"/fourteen inches/g' myFile.txt

 

49. Working with Unix paths

  • sed 's/\/some\/UNIX\/path/\/a\/new\/path/g' myFile.txt

 

50. Remove all characters from a to g from myFile.txt

  • sed 's/[a-g]//g' myFile.txt

 

51. Replace only the last match of foo with bar

  • sed 's/\(.*\)foo/bar/' myFile.txt

 

52. A tac replacement

  • sed '1!G;h;$!d'

 

53. A rev replacement

  • sed '/\n/!G;s/\(.\)\(.*\n\)/&\
    /;//D;s/.//'

 

54. A head replacement

  • sed 10q myFile.txt

 

55. A tail replacement

  • sed -e :a -e '$q;N;11,$D;ba' \
    myFile.txt

 

56. A uniq replacement

  • sed '$!N; /^\(.*\)\n$/!P; D' \
    myFile.txt

 

57. The opposite (or uniq -d equivalent)

  • sed '$!N; s/^\(.*\)\n$//;\
    t; D' myFile.txt

 

58. Equivalent to tail -n 2

  • sed '$!N;$!D' myFile.txt

 

59. ... tail -n 1 (or tail -1)

  • sed -n '$p' myFile.txt

 

60. grep equivalent

  • sed '/regexp/!d' myFile.txt

 

61. Print the line before the one matching regexp, but not the one containing the regexp

  • sed -n '/regexp/{g;1!p;};h' myFile.txt

 

62. Print the line after the one matching the regexp, butnot the one containing the regexp

  • sed -n '/regexp/{n;p;}' myFile.txt

 

63. Delete lines matching pattern

  • sed '/pattern/d' myFile.txt

 

64. Delete all blank lines from a file

  • sed '/./!d' myFile.txt

 

65. Delete all consecutive blank lines, except for the first two

  • sed '/^$/N;/\n$/N;//D' myFile.txt

 

66. Delete the last line of each paragraph

  • sed -n '/^$/{p;h;};/./{x;/./p;}'\
    myFile.txt

 

67. Remove nroff overstrikes

  • sed 's/.\x08//g' file

 

68. Get mail header

  • sed '/^$/q'

 

69. Get mail body

  • sed '1,/^$/d'

 

70. Get mail subject

  • sed '/^Subject: */!d; s///;q'

 

71. Quote mail message by inserting a "> " in front of every line

  • sed 's/^/> /'

 

72. The opposite (unquote mail message)

  • sed 's/^> //'

 

73. Remove HTML tags

  • sed -e :a -e 's/<[^>]*>//g;/</N;//ba'

 

74. Sort paragraphs of myFile.txt alphabetically

  • sed '/./{H;d;};x;s/\n/={NL}=/g'\
    myFile.txt | sort \
    | sed '1s/={NL}=//;s/={NL}=/\n/g'

 

75. Replace /usr/bin with /usr/bin/local in path.txt

  • sed 's@/usr/bin@&/local@g' path.txt

 

76. Try it and see :)

  • sed 's@^.*$@<<<&>>>@g' path.txt

 

77. Provided path.txt contains $PATH, this will echo only the first path on each line

  • sed 's/\(\/[^:]*\).*//g' path.txt

 

78. awk replacement - displays only the users from the passwd file

  • sed 's/\([^:]*\).*//' /etc/passwd

 

79. Self-explanatory

  • echo "Welcome To The Geek Stuff" | sed \
    's/\(\b[A-Z]\)/\(\)/g'
    (W)elcome (T)o (T)he (G)eek (S)tuff

 

80. Swap 'hills' for 'mountains', but only on blocks of text beginning with a blank line, and ending with a line beginning with the three characters 'END', inclusive

  • sed -e '/^$/,/^END/s/hills/\
    mountains/g' myFile.txt

 

81. View the services file without the commented lines

  • sed -e '/^#/d' /etc/services | more

 

82. Reverse order of items in the last line of path.txt

  • sed '$s@\([^:]*\):\([^:]*\):\([^:]*\
    \)@::@g' path.txt

 

83. Print 1 line of context before and after the line matching, with a line number where the matching occurs

  • sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}'\
    -e h myFile.txt

 

84. Insert a new line above every line matching regex

  • sed '/regex/{x;p;x;}' myFile.txt

 

85. Match AAA, BBB and CCC in any order

  • sed '/AAA/!d; /BBB/!d; /CCC/!d' myFile.txt

 

86. Match AAA, BBB and CCC in that order

  • sed '/AAA.*BBB.*CCC/!d' myFile.txt

 

87. Print lines 65 chars long or more

  • sed -n '/^.\{65\}/p' myFile.txt

 

88. Print lines 65 chars long or less

  • sed -n '/^.\{65\}/!p' myFile.txt

 

89. Insert blank line below every line

  • sed '/regex/G' myFile.txt

 

90. Insert blank line above and below

  • sed '/regex/{x;p;x;G;}' myFile.txt

 

91. Number lines in myFile.txt

  • sed = myFile.txt | sed 'N;s/\n/\t/'

 

92. Align text flush right

  • sed -e :a -e 's/^.\{1,78\}$/\
    &/;ta' myFile.txt

 

93. Align text center

  • sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e \
    's/\( *\)//' myFile.txt