{"id":3295,"date":"2014-07-05T18:24:30","date_gmt":"2014-07-05T10:24:30","guid":{"rendered":"http:\/\/rmohan.com\/?p=3295"},"modified":"2014-07-05T18:26:30","modified_gmt":"2014-07-05T10:26:30","slug":"sed","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3295","title":{"rendered":"SED"},"content":{"rendered":"<pre style=\"color: #000000; text-transform: none; line-height: normal; text-indent: 0px; letter-spacing: normal; font-style: normal; font-variant: normal; font-weight: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;\">FILE SPACING:\r\n\r\n # double space a file\r\n sed G\r\n\r\n # double space a file which already has blank lines in it. Output file\r\n # should contain no more than one blank line between lines of text.\r\n sed '\/^$\/d;G'\r\n\r\n # triple space a file\r\n sed 'G;G'\r\n\r\n # undo double-spacing (assumes even-numbered lines are always blank)\r\n sed 'n;d'\r\n\r\n # insert a blank line above every line which matches \"regex\"\r\n sed '\/regex\/{x;p;x;}'\r\n\r\n # insert a blank line below every line which matches \"regex\"\r\n sed '\/regex\/G'\r\n\r\n # insert a blank line above and below every line which matches \"regex\"\r\n sed '\/regex\/{x;p;x;G;}'\r\n\r\nNUMBERING:\r\n\r\n # number each line of a file (simple left alignment). Using a tab (see\r\n # note on 't' at end of file) instead of space will preserve margins.\r\n sed = filename | sed 'N;s\/n\/t\/'\r\n\r\n # number each line of a file (number on left, right-aligned)\r\n sed = filename | sed 'N; s\/^\/     \/; s\/ *(.{6,})n\/1  \/'\r\n\r\n # number each line of file, but only print numbers if line is not blank\r\n sed '\/.\/=' filename | sed '\/.\/N; s\/n\/ \/'\r\n\r\n # count lines (emulates \"wc -l\")\r\n sed -n '$='\r\n\r\nTEXT CONVERSION AND SUBSTITUTION:\r\n\r\n # IN UNIX ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format.\r\n sed 's\/.$\/\/'               # assumes that all lines end with CR\/LF\r\n sed 's\/^M$\/\/'              # in bash\/tcsh, press Ctrl-V then Ctrl-M\r\n sed 's\/x0D$\/\/'            # works on ssed, gsed 3.02.80 or higher\r\n\r\n # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.\r\n sed \"s\/$\/`echo -e r`\/\"            # command line under ksh\r\n sed 's\/$'\"\/`echo r`\/\"             # command line under bash\r\n sed \"s\/$\/`echo r`\/\"               # command line under zsh\r\n sed 's\/$\/r\/'                        # gsed 3.02.80 or higher\r\n\r\n # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.\r\n sed \"s\/$\/\/\"                          # method 1\r\n sed -n p                             # method 2\r\n\r\n # IN DOS ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format.\r\n # Can only be done with UnxUtils sed, version 4.0.7 or higher. The\r\n # UnxUtils version can be identified by the custom \"--text\" switch\r\n # which appears when you use the \"--help\" switch. Otherwise, changing\r\n # DOS newlines to Unix newlines cannot be done with sed in a DOS\r\n # environment. Use \"tr\" instead.\r\n sed \"s\/r\/\/\" infile &gt;outfile         # UnxUtils sed v4.0.7 or higher\r\n tr -d r &lt;infile &gt;outfile            # GNU tr version 1.22 or higher\r\n\r\n # delete leading whitespace (spaces, tabs) from front of each line\r\n # aligns all text flush left\r\n sed 's\/^[ t]*\/\/'                    # see note on 't' at end of file\r\n\r\n # delete trailing whitespace (spaces, tabs) from end of each line\r\n sed 's\/[ t]*$\/\/'                    # see note on 't' at end of file\r\n\r\n # delete BOTH leading and trailing whitespace from each line\r\n sed 's\/^[ t]*\/\/;s\/[ t]*$\/\/'\r\n\r\n # insert 5 blank spaces at beginning of each line (make page offset)\r\n sed 's\/^\/     \/'\r\n\r\n # align all text flush right on a 79-column width\r\n sed -e :a -e 's\/^.{1,78}$\/ &amp;\/;ta'  # set at 78 plus 1 space\r\n\r\n # center all text in the middle of 79-column width. In method 1,\r\n # spaces at the beginning of the line are significant, and trailing\r\n # spaces are appended at the end of the line. In method 2, spaces at\r\n # the beginning of the line are discarded in centering the line, and\r\n # no trailing spaces appear at the end of lines.\r\n sed  -e :a -e 's\/^.{1,77}$\/ &amp; \/;ta'                     # method 1\r\n sed  -e :a -e 's\/^.{1,77}$\/ &amp;\/;ta' -e 's\/( *)1\/1\/'  # method 2\r\n\r\n # substitute (find and replace) \"foo\" with \"bar\" on each line\r\n sed 's\/foo\/bar\/'             # replaces only 1st instance in a line\r\n sed 's\/foo\/bar\/4'            # replaces only 4th instance in a line\r\n sed 's\/foo\/bar\/g'            # replaces ALL instances in a line\r\n sed 's\/(.*)foo(.*foo)\/1bar2\/' # replace the next-to-last case\r\n sed 's\/(.*)foo\/1bar\/'            # replace only the last case\r\n\r\n # substitute \"foo\" with \"bar\" ONLY for lines which contain \"baz\"\r\n sed '\/baz\/s\/foo\/bar\/g'\r\n\r\n # substitute \"foo\" with \"bar\" EXCEPT for lines which contain \"baz\"\r\n sed '\/baz\/!s\/foo\/bar\/g'\r\n\r\n # change \"scarlet\" or \"ruby\" or \"puce\" to \"red\"\r\n sed 's\/scarlet\/red\/g;s\/ruby\/red\/g;s\/puce\/red\/g'   # most seds\r\n gsed 's\/scarlet|ruby|puce\/red\/g'                # GNU sed only\r\n\r\n # reverse order of lines (emulates \"tac\")\r\n # bug\/feature in HHsed v1.5 causes blank lines to be deleted\r\n sed '1!G;h;$!d'               # method 1\r\n sed -n '1!G;h;$p'             # method 2\r\n\r\n # reverse each character on the line (emulates \"rev\")\r\n sed '\/n\/!G;s\/(.)(.*n)\/&amp;21\/;\/\/D;s\/.\/\/'\r\n\r\n # join pairs of lines side-by-side (like \"paste\")\r\n sed '$!N;s\/n\/ \/'\r\n\r\n # if a line ends with a backslash, append the next line to it\r\n sed -e :a -e '\/$\/N; s\/n\/\/; ta'\r\n\r\n # if a line begins with an equal sign, append it to the previous line\r\n # and replace the \"=\" with a single space\r\n sed -e :a -e '$!N;s\/n=\/ \/;ta' -e 'P;D'\r\n\r\n # add commas to numeric strings, changing \"1234567\" to \"1,234,567\"\r\n gsed ':a;s\/B[0-9]{3}&gt;\/,&amp;\/;ta'                     # GNU sed\r\n sed -e :a -e 's\/(.*[0-9])([0-9]{3})\/1,2\/;ta'  # other seds\r\n\r\n # add commas to numbers with decimal points and minus signs (GNU sed)\r\n gsed -r ':a;s\/(^|[^0-9.])([0-9]+)([0-9]{3})\/12,3\/g;ta'\r\n\r\n # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)\r\n gsed '0~5G'                  # GNU sed only\r\n sed 'n;n;n;n;G;'             # other seds\r\n\r\nSELECTIVE PRINTING OF CERTAIN LINES:\r\n\r\n # print first 10 lines of file (emulates behavior of \"head\")\r\n sed 10q\r\n\r\n # print first line of file (emulates \"head -1\")\r\n sed q\r\n\r\n # print the last 10 lines of a file (emulates \"tail\")\r\n sed -e :a -e '$q;N;11,$D;ba'\r\n\r\n # print the last 2 lines of a file (emulates \"tail -2\")\r\n sed '$!N;$!D'\r\n\r\n # print the last line of a file (emulates \"tail -1\")\r\n sed '$!d'                    # method 1\r\n sed -n '$p'                  # method 2\r\n\r\n # print the next-to-the-last line of a file\r\n sed -e '$!{h;d;}' -e x              # for 1-line files, print blank line\r\n sed -e '1{$q;}' -e '$!{h;d;}' -e x  # for 1-line files, print the line\r\n sed -e '1{$d;}' -e '$!{h;d;}' -e x  # for 1-line files, print nothing\r\n\r\n # print only lines which match regular expression (emulates \"grep\")\r\n sed -n '\/regexp\/p'           # method 1\r\n sed '\/regexp\/!d'             # method 2\r\n\r\n # print only lines which do NOT match regexp (emulates \"grep -v\")\r\n sed -n '\/regexp\/!p'          # method 1, corresponds to above\r\n sed '\/regexp\/d'              # method 2, simpler syntax\r\n\r\n # print the line immediately before a regexp, but not the line\r\n # containing the regexp\r\n sed -n '\/regexp\/{g;1!p;};h'\r\n\r\n # print the line immediately after a regexp, but not the line\r\n # containing the regexp\r\n sed -n '\/regexp\/{n;p;}'\r\n\r\n # print 1 line of context before and after regexp, with line number\r\n # indicating where the regexp occurred (similar to \"grep -A1 -B1\")\r\n sed -n -e '\/regexp\/{=;x;1!p;g;$!N;p;D;}' -e h\r\n\r\n # grep for AAA and BBB and CCC (in any order)\r\n sed '\/AAA\/!d; \/BBB\/!d; \/CCC\/!d'\r\n\r\n # grep for AAA and BBB and CCC (in that order)\r\n sed '\/AAA.*BBB.*CCC\/!d'\r\n\r\n # grep for AAA or BBB or CCC (emulates \"egrep\")\r\n sed -e '\/AAA\/b' -e '\/BBB\/b' -e '\/CCC\/b' -e d    # most seds\r\n gsed '\/AAA|BBB|CCC\/!d'                        # GNU sed only\r\n\r\n # print paragraph if it contains AAA (blank lines separate paragraphs)\r\n # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below\r\n sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/!d;'\r\n\r\n # print paragraph if it contains AAA and BBB and CCC (in any order)\r\n sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/!d;\/BBB\/!d;\/CCC\/!d'\r\n\r\n # print paragraph if it contains AAA or BBB or CCC\r\n sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/b' -e '\/BBB\/b' -e '\/CCC\/b' -e d\r\n gsed '\/.\/{H;$!d;};x;\/AAA|BBB|CCC\/b;d'         # GNU sed only\r\n\r\n # print only lines of 65 characters or longer\r\n sed -n '\/^.{65}\/p'\r\n\r\n # print only lines of less than 65 characters\r\n sed -n '\/^.{65}\/!p'        # method 1, corresponds to above\r\n sed '\/^.{65}\/d'            # method 2, simpler syntax\r\n\r\n # print section of file from regular expression to end of file\r\n sed -n '\/regexp\/,$p'\r\n\r\n # print section of file based on line numbers (lines 8-12, inclusive)\r\n sed -n '8,12p'               # method 1\r\n sed '8,12!d'                 # method 2\r\n\r\n # print line number 52\r\n sed -n '52p'                 # method 1\r\n sed '52!d'                   # method 2\r\n sed '52q;d'                  # method 3, efficient on large files\r\n\r\n # beginning at line 3, print every 7th line\r\n gsed -n '3~7p'               # GNU sed only\r\n sed -n '3,${p;n;n;n;n;n;n;}' # other seds\r\n\r\n # print section of file between two regular expressions (inclusive)\r\n sed -n '\/Iowa\/,\/Montana\/p'             # case sensitive\r\n\r\nSELECTIVE DELETION OF CERTAIN LINES:\r\n\r\n # print all of file EXCEPT section between 2 regular expressions\r\n sed '\/Iowa\/,\/Montana\/d'\r\n\r\n # delete duplicate, consecutive lines from a file (emulates \"uniq\").\r\n # First line in a set of duplicate lines is kept, rest are deleted.\r\n sed '$!N; \/^(.*)n1$\/!P; D'\r\n\r\n # delete duplicate, nonconsecutive lines from a file. Beware not to\r\n # overflow the buffer size of the hold space, or else use GNU sed.\r\n sed -n 'G; s\/n\/&amp;&amp;\/; \/^([ -~]*n).*n1\/d; s\/n\/\/; h; P'\r\n\r\n # delete all lines except duplicate lines (emulates \"uniq -d\").\r\n sed '$!N; s\/^(.*)n1$\/1\/; t; D'\r\n\r\n # delete the first 10 lines of a file\r\n sed '1,10d'\r\n\r\n # delete the last line of a file\r\n sed '$d'\r\n\r\n # delete the last 2 lines of a file\r\n sed 'N;$!P;$!D;$d'\r\n\r\n # delete the last 10 lines of a file\r\n sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1\r\n sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2\r\n\r\n # delete every 8th line\r\n gsed '0~8d'                           # GNU sed only\r\n sed 'n;n;n;n;n;n;n;d;'                # other seds\r\n\r\n # delete lines matching pattern\r\n sed '\/pattern\/d'\r\n\r\n # delete ALL blank lines from a file (same as \"grep '.' \")\r\n sed '\/^$\/d'                           # method 1\r\n sed '\/.\/!d'                           # method 2\r\n\r\n # delete all CONSECUTIVE blank lines from file except the first; also\r\n # deletes all blank lines from top and end of file (emulates \"cat -s\")\r\n sed '\/.\/,\/^$\/!d'          # method 1, allows 0 blanks at top, 1 at EOF\r\n sed '\/^$\/N;\/n$\/D'        # method 2, allows 1 blank at top, 0 at EOF\r\n\r\n # delete all CONSECUTIVE blank lines from file except the first 2:\r\n sed '\/^$\/N;\/n$\/N;\/\/D'\r\n\r\n # delete all leading blank lines at top of file\r\n sed '\/.\/,$!d'\r\n\r\n # delete all trailing blank lines at end of file\r\n sed -e :a -e '\/^n*$\/{$d;N;ba' -e '}'  # works on all seds\r\n sed -e :a -e '\/^n*$\/N;\/n$\/ba'        # ditto, except for gsed 3.02.*\r\n\r\n # delete the last line of each paragraph\r\n sed -n '\/^$\/{p;h;};\/.\/{x;\/.\/p;}'\r\n\r\nSPECIAL APPLICATIONS:\r\n\r\n # remove nroff overstrikes (char, backspace) from man pages. The 'echo'\r\n # command may need an -e switch if you use Unix System V or bash shell.\r\n sed \"s\/.`echo b`\/\/g\"    # double quotes required for Unix environment\r\n sed 's\/.^H\/\/g'             # in bash\/tcsh, press Ctrl-V and then Ctrl-H\r\n sed 's\/.x08\/\/g'           # hex expression for sed 1.5, GNU sed, ssed\r\n\r\n # get Usenet\/e-mail message header\r\n sed '\/^$\/q'                # deletes everything after first blank line\r\n\r\n # get Usenet\/e-mail message body\r\n sed '1,\/^$\/d'              # deletes everything up to first blank line\r\n\r\n # get Subject header, but remove initial \"Subject: \" portion\r\n sed '\/^Subject: *\/!d; s\/\/\/;q'\r\n\r\n # get return address header\r\n sed '\/^Reply-To:\/q; \/^From:\/h; \/.\/d;g;q'\r\n\r\n # parse out the address proper. Pulls out the e-mail address by itself\r\n # from the 1-line return address header (see preceding script)\r\n sed 's\/ *(.*)\/\/; s\/&gt;.*\/\/; s\/.*[:&lt;] *\/\/'\r\n\r\n # add a leading angle bracket and space to each line (quote a message)\r\n sed 's\/^\/&gt; \/'\r\n\r\n # delete leading angle bracket &amp; space from each line (unquote a message)\r\n sed 's\/^&gt; \/\/'\r\n\r\n # remove most HTML tags (accommodates multiple-line tags)\r\n sed -e :a -e 's\/&lt;[^&gt;]*&gt;\/\/g;\/&lt;\/N;\/\/ba'\r\n\r\n # extract multi-part uuencoded binaries, removing extraneous header\r\n # info, so that only the uuencoded portion remains. Files passed to\r\n # sed must be passed in the proper order. Version 1 can be entered\r\n # from the command line; version 2 can be made into an executable\r\n # Unix shell script. (Modified from a script by Rahul Dhesi.)\r\n sed '\/^end\/,\/^begin\/d' file1 file2 ... fileX | uudecode   # vers. 1\r\n sed '\/^end\/,\/^begin\/d' \"$@\" | uudecode                    # vers. 2\r\n\r\n # sort paragraphs of file alphabetically. Paragraphs are separated by blank\r\n # lines. GNU sed uses v for vertical tab, or any unique char will do.\r\n sed '\/.\/{H;d;};x;s\/n\/={NL}=\/g' file | sort | sed '1s\/={NL}=\/\/;s\/={NL}=\/n\/g'\r\n gsed '\/.\/{H;d};x;y\/n\/v\/' file | sort | sed '1s\/v\/\/;y\/v\/n\/'\r\n\r\n # zip up each .TXT file individually, deleting the source file and\r\n # setting the name of each .ZIP file to the basename of the .TXT file\r\n # (under DOS: the \"dir \/b\" switch returns bare filenames in all caps).\r\n echo @echo off &gt;zipup.bat\r\n dir \/b *.txt | sed \"s\/^(.*).TXT\/pkzip -mo 1 1.TXT\/\" &gt;&gt;zipup.bat<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8216;\/^$\/d;G&#8217; # triple space a file sed &#8216;G;G&#8217; # undo double-spacing (assumes even-numbered lines are always blank) sed [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3295"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3295"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3295\/revisions"}],"predecessor-version":[{"id":3296,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3295\/revisions\/3296"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}