Sed Cheat Sheet



  1. Linux Sed Command Cheat Sheet
  2. Sed Stream Editor Cheat Sheet

Here's the next cheat sheet I've created - cheat sheet of sed, the Superman of stream editing. It has come handy 101 times for me because sed is not what I use daily and after some time of not using the sed commands tend to fade away from my mind and I have to review them quickly. This cheat sheet. FILE SPACING: # double space a file sed G # double space a file which already has blank lines in it. Output file # should contain no more than one blank line between lines of text. Sed ‘/^$/d;G’ # triple space a file sed ‘G;G’ # undo double-spacing (assumes even-numbered lines are always blank) sed Continue reading Sed cheat sheet / one-line sed. A programming cheat sheet. Sed general form sed range from(,range to)?command syntax a # match the letter a.

sed is an extremely powerful stream editor. The web is full of useful sed one liners.

Remote download wireless adapter lg. This post will be an ever growing list of sed An wf100 wifi dongle. one liners I have found useful.

Find and Replace

Find and remove all instances of TEXT:

Find and remove all spaces:

Delete all blank lines from a file

Delete leading whitespace from front of each line

Delete trailing whitespace from end of each line

Delete leading and trailing whitespace from each line

Insert a line of text before a line

Change configuration file values

Sed Cheat Sheet

Change BRIDGE_HOTPLUG=yes to BRIDGE_HOTPLUG=no no matter what it is already set to: Nijamellam maranthu pochu song heroine name.

Change PermitRootLogin no to PermitRootLogin yes no matter what it is already set to:

Wrap Value in Double Quotes

Change date=2015 to date='2015”:

Remove word wrap

Display links between href tags

Linux Sed Command Cheat Sheet

Lower case all href=”*” tags

Lower case all img src=”*” tags

Lower case all href=”” tags but exclude href='http” tags

Sed Cheat Sheet

Lower case all img src=”” tags but exclude img src='http” tags

Remove ‘%20’ only from lines matching href=”*”

References

Sed Stream Editor Cheat Sheet

  1. sed G
  2. # double space a file which already has blank lines in it. Output file
  3. # should contain no more than one blank line between lines of text.
  4. sed‘G;G’
  5. # undo double-spacing (assumes even-numbered lines are always blank)
  6. # insert a blank line above every line which matches “regex”
  7. # insert a blank line below every line which matches “regex”
  8. # insert a blank line above and below every line which matches “regex”
  9. # number each line of a file (simple left alignment). Using a tab (see
  10. # note on ‘t’ at end of file) instead of space will preserve margins.
  11. # number each line of a file (number on left, right-aligned)
  12. # number each line of file, but only print numbers if line is not blank
  13. sed -n ‘$=’
  14. TEXT CONVERSION AND SUBSTITUTION:
  15. # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
  16. sed‘s/.$//’# assumes that all lines end with CR/LF
  17. sed‘s/^M$//’# in bash/tcsh, press Ctrl-V then Ctrl-M
  18. sed‘s/x0D$//’# works on ssed, gsed 3.02.80 or higher
  19. # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.
  20. sed‘s/$’“/`echo r`/”# command line under bash
  21. sed‘s/$/r/’# gsed 3.02.80 or higher
  22. # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.
  23. sed -n p # method 2
  24. # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format.
  25. # Can only be done with UnxUtils sed, version 4.0.7 or higher. The
  26. # UnxUtils version can be identified by the custom “–text” switch
  27. # which appears when you use the “–help” switch. Otherwise, changing
  28. # DOS newlines to Unix newlines cannot be done with sed in a DOS
  29. sed“s/r//” infile >outfile # UnxUtils sed v4.0.7 or higher
  30. tr -d r <infile >outfile # GNU tr version 1.22 or higher
  31. # delete leading whitespace (spaces, tabs) from front of each line
  32. sed‘s/^[ t]*//’# see note on ‘t’ at end of file
  33. # delete trailing whitespace (spaces, tabs) from end of each line
  34. # delete BOTH leading and trailing whitespace from each line
  35. # insert 5 blank spaces at beginning of each line (make page offset)
  36. sed -e :a -e ‘s/^.{1,78}$/ &/;ta’# set at 78 plus 1 space
  37. # center all text in the middle of 79-column width. In method 1,
  38. # spaces at the beginning of the line are significant, and trailing
  39. # spaces are appended at the end of the line. In method 2, spaces at
  40. # the beginning of the line are discarded in centering the line, and
  41. sed -e :a -e ‘s/^.{1,77}$/ & /;ta’# method 1
  42. sed -e :a -e ‘s/^.{1,77}$/ &/;ta’ -e ‘s/( *)1/1/’# method 2
  43. # substitute (find and replace) “foo” with “bar” on each line
  44. sed‘s/foo/bar/’# replaces only 1st instance in a line
  45. sed‘s/foo/bar/4’# replaces only 4th instance in a line
  46. sed‘s/foo/bar/g’# replaces ALL instances in a line
  47. sed‘s/(.*)foo(.*foo)/1bar2/’# replace the next-to-last case
  48. # substitute “foo” with “bar” ONLY for lines which contain “baz”
  49. # substitute “foo” with “bar” EXCEPT for lines which contain “baz”
  50. sed‘s/scarlet/red/g;s/ruby/red/g;s/puce/red/g’# most seds
  51. # bug/feature in HHsed v1.5 causes blank lines to be deleted
  52. sed -n ‘1!G;h;$p’# method 2
  53. # reverse each character on the line (emulates “rev”)
  54. sed‘$!N;s/n/ /’
  55. # if a line ends with a backslash, append the next line to it
  56. # if a line begins with an equal sign, append it to the previous line
  57. sed -e :a -e ‘$!N;s/n=/ /;ta’ -e ‘P;D’
  58. # add commas to numeric strings, changing “1234567” to “1,234,567”
  59. sed -e :a -e ‘s/(.*[0-9])([0-9]{3})/1,2/;ta’# other seds
  60. # add commas to numbers with decimal points and minus signs (GNU sed)
  61. gsed -r ‘:a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/12,3/g;ta’
  62. # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
  63. sed‘n;n;n;n;G;’# other seds
  64. SELECTIVE PRINTING OF CERTAIN LINES:
  65. # print first 10 lines of file (emulates behavior of “head”)
  66. sed q
  67. # print the last 10 lines of a file (emulates “tail”)
  68. # print the last 2 lines of a file (emulates “tail -2”)
  69. # print the last line of a file (emulates “tail -1”)
  70. sed -n ‘$p’# method 2
  71. # print the next-to-the-last line of a file
  72. sed -e ‘$!{h;d;}’ -e x # for 1-line files, print blank line
  73. sed -e ‘1{$q;}’ -e ‘$!{h;d;}’ -e x # for 1-line files, print the line
  74. sed -e ‘1{$d;}’ -e ‘$!{h;d;}’ -e x # for 1-line files, print nothing
  75. # print only lines which match regular expression (emulates “grep”)
  76. sed‘/regexp/!d’# method 2
  77. # print only lines which do NOT match regexp (emulates “grep -v”)
  78. sed -n ‘/regexp/!p’# method 1, corresponds to above
  79. # print the line immediately before a regexp, but not the line
  80. sed -n ‘/regexp/{g;1!p;};h’
  81. # print the line immediately after a regexp, but not the line
  82. sed -n ‘/regexp/{n;p;}’
  83. # print 1 line of context before and after regexp, with line number
  84. # indicating where the regexp occurred (similar to “grep -A1 -B1”)
  85. sed‘/AAA/!d; /BBB/!d; /CCC/!d’
  86. # grep for AAA and BBB and CCC (in that order)
  87. sed -e ‘/AAA/b’ -e ‘/BBB/b’ -e ‘/CCC/b’ -e d # most seds
  88. # print paragraph if it contains AAA (blank lines separate paragraphs)
  89. # HHsed v1.5 must insert a ‘G;’ after ‘x;’ in the next 3 scripts below
  90. # print paragraph if it contains AAA and BBB and CCC (in any order)
  91. sed -e ‘/./{H;$!d;}’ -e ‘x;/AAA/!d;/BBB/!d;/CCC/!d’
  92. # print paragraph if it contains AAA or BBB or CCC
  93. sed -e ‘/./{H;$!d;}’ -e ‘x;/AAA/b’ -e ‘/BBB/b’ -e ‘/CCC/b’ -e d
  94. gsed ‘/./{H;$!d;};x;/AAA|BBB|CCC/b;d’# GNU sed only
  95. # print only lines of 65 characters or longer
  96. sed -n ‘/^.{65}/!p’# method 1, corresponds to above
  97. # print section of file from regular expression to end of file
  98. # print section of file based on line numbers (lines 8-12, inclusive)
  99. sed‘8,12!d’# method 2
  100. # print line number 52
  101. sed’52!d’# method 2
  102. gsed -n ‘3~7p’# GNU sed only
  103. # print section of file between two regular expressions (inclusive)
  104. # print all of file EXCEPT section between 2 regular expressions
  105. # delete duplicate, consecutive lines from a file (emulates “uniq”).
  106. # First line in a set of duplicate lines is kept, rest are deleted.
  107. # delete duplicate, nonconsecutive lines from a file. Beware not to
  108. # overflow the buffer size of the hold space, or else use GNU sed.
  109. sed -n ‘G; s/n/&&/; /^([ -~]*n).*n1/d; s/n//; h; P’
  110. # delete all lines except duplicate lines (emulates “uniq -d”).
  111. sed‘1,10d’
  112. # delete the last line of a file
  113. sed‘N;$!P;$!D;$d’
  114. # delete the last 10 lines of a file
  115. sed -n -e :a -e ‘1,10!{P;N;D;};N;ba’# method 2
  116. # delete every 8th line
  117. sed‘n;n;n;n;n;n;n;d;’# other seds
  118. # delete lines matching pattern
  119. # delete ALL blank lines from a file (same as “grep ‘.’ “)
  120. sed‘/./!d’# method 2
  121. # delete all CONSECUTIVE blank lines from file except the first; also
  122. # deletes all blank lines from top and end of file (emulates “cat -s”)
  123. sed‘/./,/^$/!d’# method 1, allows 0 blanks at top, 1 at EOF
  124. sed‘/^$/N;/n$/D’# method 2, allows 1 blank at top, 0 at EOF
  125. # delete all CONSECUTIVE blank lines from file except the first 2:
  126. sed‘/./,$!d’
  127. # delete all trailing blank lines at end of file
  128. sed -e :a -e ‘/^n*$/{$d;N;ba’ -e ‘}’# works on all seds
  129. sed -e :a -e ‘/^n*$/N;/n$/ba’# ditto, except for gsed 3.02.*
  130. # delete the last line of each paragraph
  131. # remove nroff overstrikes (char, backspace) from man pages. The ‘echo’
  132. # command may need an -e switch if you use Unix System V or bash shell.
  133. sed“s/.`echo b`//g”# double quotes required for Unix environment
  134. sed‘s/.^H//g’# in bash/tcsh, press Ctrl-V and then Ctrl-H
  135. sed‘s/.x08//g’# hex expression for sed 1.5, GNU sed, ssed
  136. # get Usenet/e-mail message header
  137. sed‘/^$/q’# deletes everything after first blank line
  138. # get Usenet/e-mail message body
  139. sed‘1,/^$/d’# deletes everything up to first blank line
  140. # get Subject header, but remove initial “Subject: ” portion
  141. sed‘/^Reply-To:/q; /^From:/h; /./d;g;q’
  142. # parse out the address proper. Pulls out the e-mail address by itself
  143. # from the 1-line return address header (see preceding script)
  144. # add a leading angle bracket and space to each line (quote a message)
  145. # delete leading angle bracket & space from each line (unquote a message)
  146. # remove most HTML tags (accommodates multiple-line tags)
  147. # extract multi-part uuencoded binaries, removing extraneous header
  148. # info, so that only the uuencoded portion remains. Files passed to
  149. # sed must be passed in the proper order. Version 1 can be entered
  150. # from the command line; version 2 can be made into an executable
  151. # Unix shell script. (Modified from a script by Rahul Dhesi.)
  152. sed‘/^end/,/^begin/d’ file1 file2 … fileX | uudecode # vers. 1
  153. # sort paragraphs of file alphabetically. Paragraphs are separated by blank
  154. # lines. GNU sed uses v for vertical tab, or any unique char will do.
  155. sed‘/./{H;d;};x;s/n/={NL}=/g’file | sort | sed‘1s/={NL}=//;s/={NL}=/n/g’
  156. gsed ‘/./{H;d};x;y/n/v/’file | sort | sed‘1s/v//;y/v/n/’
  157. # zip up each .TXT file individually, deleting the source file and
  158. # setting the name of each .ZIP file to the basename of the .TXT file
  159. # (under DOS: the “dir /b” switch returns bare filenames in all caps).
  160. dir /b *.txt | sed“s/^(.*).TXT/pkzip -mo 1 1.TXT/” >>zipup.bat