Skip to content

Sed cheat sheet / one-line sed

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