{"id":362,"date":"2011-06-15T15:07:00","date_gmt":"2011-06-16T00:07:00","guid":{"rendered":"\/blog\/?p=362"},"modified":"2023-09-21T09:38:56","modified_gmt":"2023-09-21T00:38:56","slug":"sed-%ec%82%ac%ec%9a%a9%eb%b0%a9%eb%b2%95-%ec%a0%95%eb%a6%ac","status":"publish","type":"post","link":"https:\/\/hasu0707.duckdns.org\/blog\/?p=362","title":{"rendered":"sed \uc0ac\uc6a9\ubc29\ubc95 \uc815\ub9ac"},"content":{"rendered":"\n<span style=\"font-family: GulimChe;\">-------------------------------------------------------------------------<\/span><br \/>\n<span style=\"font-family: GulimChe;\">USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dec. 29, 2005<\/span><br \/>\n<span style=\"font-family: GulimChe;\">Compiled by Eric Pement - pemente[at]northpark[dot]edu&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; version 5.5<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">Latest version of this file (in English) is usually at:<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; http:\/\/sed.sourceforge.net\/sed1line.txt<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; http:\/\/www.pement.org\/sed\/sed1line.txt<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">This file will also available in other languages:<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Chinese&nbsp;&nbsp;&nbsp;&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_zh-CN.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Czech&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_cz.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Dutch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_nl.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; French&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_fr.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; German&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_de.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Italian&nbsp;&nbsp;&nbsp;&nbsp; - (pending)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Portuguese&nbsp; - http:\/\/sed.sourceforge.net\/sed1line_pt-BR.html<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp; Spanish&nbsp;&nbsp;&nbsp;&nbsp; - (pending)<\/span><br \/>\n<br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">FILE SPACING:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# double space a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed G<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# double space a file which already has blank lines in it. Output file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# should contain no more than one blank line between lines of text.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^$\/d;G'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# triple space a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 'G;G'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# undo double-spacing (assumes even-numbered lines are always blank)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 'n;d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# insert a blank line above every line which matches \"regex\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/regex\/{x;p;x;}'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# insert a blank line below every line which matches \"regex\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/regex\/G'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# insert a blank line above and below every line which matches \"regex\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/regex\/{x;p;x;G;}'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">NUMBERING:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# number each line of a file (simple left alignment). Using a tab (see<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# note on '\\t' at end of file) instead of space will preserve margins.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed = filename | sed 'N;s\/\\n\/\\t\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# number each line of a file (number on left, right-aligned)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed = filename | sed 'N; s\/^\/&nbsp;&nbsp;&nbsp;&nbsp; \/; s\/ *\\(.\\{6,\\}\\)\\n\/\\1&nbsp; \/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# number each line of file, but only print numbers if line is not blank<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/.\/=' filename | sed '\/.\/N; s\/\\n\/ \/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# count lines (emulates \"wc -l\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '$='<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">TEXT CONVERSION AND SUBSTITUTION:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# IN UNIX ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/.$\/\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # assumes that all lines end with CR\/LF<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^M$\/\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # in bash\/tcsh, press Ctrl-V then Ctrl-M<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/\\x0D$\/\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # works on ssed, gsed 3.02.80 or higher<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed \"s\/$\/`echo -e \\\\\\r`\/\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # command line under ksh<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/$'\"\/`echo \\\\\\r`\/\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # command line under bash<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed \"s\/$\/`echo \\\\\\r`\/\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # command line under zsh<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/$\/\\r\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # gsed 3.02.80 or higher<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed \"s\/$\/\/\"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n p&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# IN DOS ENVIRONMENT: convert DOS newlines (CR\/LF) to Unix format.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# Can only be done with UnxUtils sed, version 4.0.7 or higher. The<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# UnxUtils version can be identified by the custom \"--text\" switch<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# which appears when you use the \"--help\" switch. Otherwise, changing<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# DOS newlines to Unix newlines cannot be done with sed in a DOS<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# environment. Use \"tr\" instead.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed \"s\/\\r\/\/\" infile &gt;outfile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # UnxUtils sed v4.0.7 or higher<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;tr -d \\r &lt;infile &gt;outfile&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU tr version 1.22 or higher<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete leading whitespace (spaces, tabs) from front of each line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# aligns all text flush left<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^[ \\t]*\/\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # see note on '\\t' at end of file<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete trailing whitespace (spaces, tabs) from end of each line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/[ \\t]*$\/\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # see note on '\\t' at end of file<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete BOTH leading and trailing whitespace from each line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^[ \\t]*\/\/;s\/[ \\t]*$\/\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# insert 5 blank spaces at beginning of each line (make page offset)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^\/&nbsp;&nbsp;&nbsp;&nbsp; \/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# align all text flush right on a 79-column width<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e 's\/^.\\{1,78\\}$\/ &amp;\/;ta'&nbsp; # set at 78 plus 1 space<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# center all text in the middle of 79-column width. In method 1,<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# spaces at the beginning of the line are significant, and trailing<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# spaces are appended at the end of the line. In method 2, spaces at<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# the beginning of the line are discarded in centering the line, and<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# no trailing spaces appear at the end of lines.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed&nbsp; -e :a -e 's\/^.\\{1,77\\}$\/ &amp; \/;ta'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed&nbsp; -e :a -e 's\/^.\\{1,77\\}$\/ &amp;\/;ta' -e 's\/\\( *\\)\\1\/\\1\/'&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# substitute (find and replace) \"foo\" with \"bar\" on each line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/foo\/bar\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # replaces only 1st instance in a line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/foo\/bar\/4'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # replaces only 4th instance in a line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/foo\/bar\/g'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # replaces ALL instances in a line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/\\(.*\\)foo\\(.*foo\\)\/\\1bar\\2\/' # replace the next-to-last case<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/\\(.*\\)foo\/\\1bar\/'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # replace only the last case<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# substitute \"foo\" with \"bar\" ONLY for lines which contain \"baz\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/baz\/s\/foo\/bar\/g'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# substitute \"foo\" with \"bar\" EXCEPT for lines which contain \"baz\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/baz\/!s\/foo\/bar\/g'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# change \"scarlet\" or \"ruby\" or \"puce\" to \"red\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/scarlet\/red\/g;s\/ruby\/red\/g;s\/puce\/red\/g'&nbsp;&nbsp; # most seds<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed 's\/scarlet\\|ruby\\|puce\/red\/g'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# reverse order of lines (emulates \"tac\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# bug\/feature in HHsed v1.5 causes blank lines to be deleted<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '1!G;h;$!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '1!G;h;$p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# reverse each character on the line (emulates \"rev\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/\\n\/!G;s\/\\(.\\)\\(.*\\n\\)\/&amp;\\2\\1\/;\/\/D;s\/.\/\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# join pairs of lines side-by-side (like \"paste\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$!N;s\/\\n\/ \/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# if a line ends with a backslash, append the next line to it<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '\/\\\\$\/N; s\/\\\\\\n\/\/; ta'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# if a line begins with an equal sign, append it to the previous line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# and replace the \"=\" with a single space<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '$!N;s\/\\n=\/ \/;ta' -e 'P;D'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# add commas to numeric strings, changing \"1234567\" to \"1,234,567\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed ':a;s\/\\B[0-9]\\{3\\}\\&gt;\/,&amp;\/;ta'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e 's\/\\(.*[0-9]\\)\\([0-9]\\{3\\}\\)\/\\1,\\2\/;ta'&nbsp; # other seds<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# add commas to numbers with decimal points and minus signs (GNU sed)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed -r ':a;s\/(^|[^0-9.])([0-9]+)([0-9]{3})\/\\1\\2,\\3\/g;ta'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed '0~5G'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 'n;n;n;n;G;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # other seds<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">SELECTIVE PRINTING OF CERTAIN LINES:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print first 10 lines of file (emulates behavior of \"head\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 10q<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print first line of file (emulates \"head -1\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed q<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the last 10 lines of a file (emulates \"tail\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '$q;N;11,$D;ba'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the last 2 lines of a file (emulates \"tail -2\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$!N;$!D'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the last line of a file (emulates \"tail -1\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '$p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the next-to-the-last line of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '$!{h;d;}' -e x&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # for 1-line files, print blank line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '1{$q;}' -e '$!{h;d;}' -e x&nbsp; # for 1-line files, print the line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '1{$d;}' -e '$!{h;d;}' -e x&nbsp; # for 1-line files, print nothing<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print only lines which match regular expression (emulates \"grep\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/regexp\/p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/regexp\/!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print only lines which do NOT match regexp (emulates \"grep -v\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/regexp\/!p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1, corresponds to above<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/regexp\/d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2, simpler syntax<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the line immediately before a regexp, but not the line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# containing the regexp<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/regexp\/{g;1!p;};h'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print the line immediately after a regexp, but not the line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# containing the regexp<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/regexp\/{n;p;}'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print 1 line of context before and after regexp, with line number<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# indicating where the regexp occurred (similar to \"grep -A1 -B1\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n -e '\/regexp\/{=;x;1!p;g;$!N;p;D;}' -e h<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# grep for AAA and BBB and CCC (in any order)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/AAA\/!d; \/BBB\/!d; \/CCC\/!d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# grep for AAA and BBB and CCC (in that order)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/AAA.*BBB.*CCC\/!d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# grep for AAA or BBB or CCC (emulates \"egrep\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '\/AAA\/b' -e '\/BBB\/b' -e '\/CCC\/b' -e d&nbsp;&nbsp;&nbsp; # most seds<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed '\/AAA\\|BBB\\|CCC\/!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print paragraph if it contains AAA (blank lines separate paragraphs)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/!d;'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print paragraph if it contains AAA and BBB and CCC (in any order)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/!d;\/BBB\/!d;\/CCC\/!d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print paragraph if it contains AAA or BBB or CCC<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e '\/.\/{H;$!d;}' -e 'x;\/AAA\/b' -e '\/BBB\/b' -e '\/CCC\/b' -e d<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed '\/.\/{H;$!d;};x;\/AAA\\|BBB\\|CCC\/b;d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print only lines of 65 characters or longer<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/^.\\{65\\}\/p'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print only lines of less than 65 characters<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/^.\\{65\\}\/!p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1, corresponds to above<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^.\\{65\\}\/d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2, simpler syntax<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print section of file from regular expression to end of file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/regexp\/,$p'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print section of file based on line numbers (lines 8-12, inclusive)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '8,12p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '8,12!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print line number 52<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '52p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '52!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '52q;d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 3, efficient on large files<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# beginning at line 3, print every 7th line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed -n '3~7p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '3,${p;n;n;n;n;n;n;}' # other seds<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print section of file between two regular expressions (inclusive)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/Iowa\/,\/Montana\/p'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # case sensitive<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">SELECTIVE DELETION OF CERTAIN LINES:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# print all of file EXCEPT section between 2 regular expressions<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/Iowa\/,\/Montana\/d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete duplicate, consecutive lines from a file (emulates \"uniq\").<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# First line in a set of duplicate lines is kept, rest are deleted.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$!N; \/^\\(.*\\)\\n\\1$\/!P; D'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete duplicate, nonconsecutive lines from a file. Beware not to<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# overflow the buffer size of the hold space, or else use GNU sed.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n 'G; s\/\\n\/&amp;&amp;\/; \/^\\([ -~]*\\n\\).*\\n\\1\/d; s\/\\n\/\/; h; P'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete all lines except duplicate lines (emulates \"uniq -d\").<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$!N; s\/^\\(.*\\)\\n\\1$\/\\1\/; t; D'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete the first 10 lines of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '1,10d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete the last line of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '$d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete the last 2 lines of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 'N;$!P;$!D;$d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete the last 10 lines of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '$d;N;2,10ba' -e 'P;D'&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n -e :a -e '1,10!{P;N;D;};N;ba'&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete every 8th line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed '0~8d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # GNU sed only<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 'n;n;n;n;n;n;n;d;'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # other seds<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete lines matching pattern<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/pattern\/d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete ALL blank lines from a file (same as \"grep '.' \")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^$\/d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/.\/!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete all CONSECUTIVE blank lines from file except the first; also<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# deletes all blank lines from top and end of file (emulates \"cat -s\")<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/.\/,\/^$\/!d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 1, allows 0 blanks at top, 1 at EOF<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^$\/N;\/\\n$\/D'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # method 2, allows 1 blank at top, 0 at EOF<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete all CONSECUTIVE blank lines from file except the first 2:<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^$\/N;\/\\n$\/N;\/\/D'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete all leading blank lines at top of file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/.\/,$!d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete all trailing blank lines at end of file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '\/^\\n*$\/{$d;N;ba' -e '}'&nbsp; # works on all seds<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e '\/^\\n*$\/N;\/\\n$\/ba'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # ditto, except for gsed 3.02.*<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete the last line of each paragraph<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -n '\/^$\/{p;h;};\/.\/{x;\/.\/p;}'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">SPECIAL APPLICATIONS:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# remove nroff overstrikes (char, backspace) from man pages. The 'echo'<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# command may need an -e switch if you use Unix System V or bash shell.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed \"s\/.`echo \\\\\\b`\/\/g\"&nbsp;&nbsp;&nbsp; # double quotes required for Unix environment<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/.^H\/\/g'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # in bash\/tcsh, press Ctrl-V and then Ctrl-H<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/.\\x08\/\/g'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # hex expression for sed 1.5, GNU sed, ssed<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# get Usenet\/e-mail message header<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^$\/q'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # deletes everything after first blank line<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# get Usenet\/e-mail message body<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '1,\/^$\/d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # deletes everything up to first blank line<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# get Subject header, but remove initial \"Subject: \" portion<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^Subject: *\/!d; s\/\/\/;q'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# get return address header<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^Reply-To:\/q; \/^From:\/h; \/.\/d;g;q'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# parse out the address proper. Pulls out the e-mail address by itself<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# from the 1-line return address header (see preceding script)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/ *(.*)\/\/; s\/&gt;.*\/\/; s\/.*[:&lt;] *\/\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# add a leading angle bracket and space to each line (quote a message)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^\/&gt; \/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# delete leading angle bracket &amp; space from each line (unquote a message)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed 's\/^&gt; \/\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# remove most HTML tags (accommodates multiple-line tags)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed -e :a -e 's\/&lt;[^&gt;]*&gt;\/\/g;\/&lt;\/N;\/\/ba'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# extract multi-part uuencoded binaries, removing extraneous header<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# info, so that only the uuencoded portion remains. Files passed to<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# sed must be passed in the proper order. Version 1 can be entered<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# from the command line; version 2 can be made into an executable<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# Unix shell script. (Modified from a script by Rahul Dhesi.)<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^end\/,\/^begin\/d' file1 file2 ... fileX | uudecode&nbsp;&nbsp; # vers. 1<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/^end\/,\/^begin\/d' \"$@\" | uudecode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # vers. 2<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# sort paragraphs of file alphabetically. Paragraphs are separated by blank<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# lines. GNU sed uses \\v for vertical tab, or any unique char will do.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '\/.\/{H;d;};x;s\/\\n\/={NL}=\/g' file | sort | sed '1s\/={NL}=\/\/;s\/={NL}=\/\\n\/g'<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;gsed '\/.\/{H;d};x;y\/\\n\/\\v\/' file | sort | sed '1s\/\\v\/\/;y\/\\v\/\\n\/'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# zip up each .TXT file individually, deleting the source file and<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# setting the name of each .ZIP file to the basename of the .TXT file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;# (under DOS: the \"dir \/b\" switch returns bare filenames in all caps).<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;echo @echo off &gt;zipup.bat<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;dir \/b *.txt | sed \"s\/^\\(.*\\)\\.TXT\/pkzip -mo \\1 \\1.TXT\/\" &gt;&gt;zipup.bat<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">TYPICAL USE: Sed takes one or more editing commands and applies all of<\/span><br \/>\n<span style=\"font-family: GulimChe;\">them, in sequence, to each line of input. After all the commands have<\/span><br \/>\n<span style=\"font-family: GulimChe;\">been applied to the first input line, that line is output and a second<\/span><br \/>\n<span style=\"font-family: GulimChe;\">input line is taken for processing, and the cycle repeats. The<\/span><br \/>\n<span style=\"font-family: GulimChe;\">preceding examples assume that input comes from the standard input<\/span><br \/>\n<span style=\"font-family: GulimChe;\">device (i.e, the console, normally this will be piped input). One or<\/span><br \/>\n<span style=\"font-family: GulimChe;\">more filenames can be appended to the command line if the input does<\/span><br \/>\n<span style=\"font-family: GulimChe;\">not come from stdin. Output is sent to stdout (the screen). Thus:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;cat filename | sed '10q'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # uses piped input<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '10q' filename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # same effect, avoids a useless \"cat\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;sed '10q' filename &gt; newfile&nbsp;&nbsp;&nbsp; # redirects output to disk<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">For additional syntax instructions, including the way to apply editing<\/span><br \/>\n<span style=\"font-family: GulimChe;\">commands from a disk file instead of the command line, consult \"sed &amp;<\/span><br \/>\n<span style=\"font-family: GulimChe;\">awk, 2nd Edition,\" by Dale Dougherty and Arnold Robbins (O'Reilly,<\/span><br \/>\n<span style=\"font-family: GulimChe;\">1997; http:\/\/www.ora.com), \"UNIX Text Processing,\" by Dale Dougherty<\/span><br \/>\n<span style=\"font-family: GulimChe;\">and Tim O'Reilly (Hayden Books, 1987) or the tutorials by Mike Arst<\/span><br \/>\n<span style=\"font-family: GulimChe;\">distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power<\/span><br \/>\n<span style=\"font-family: GulimChe;\">of sed, one must understand \"regular expressions.\" For this, see<\/span><br \/>\n<span style=\"font-family: GulimChe;\">\"Mastering Regular Expressions\" by Jeffrey Friedl (O'Reilly, 1997).<\/span><br \/>\n<span style=\"font-family: GulimChe;\">The manual (\"man\") pages on Unix systems may be helpful (try \"man<\/span><br \/>\n<span style=\"font-family: GulimChe;\">sed\", \"man regexp\", or the subsection on regular expressions in \"man<\/span><br \/>\n<span style=\"font-family: GulimChe;\">ed\"), but man pages are notoriously difficult. They are not written to<\/span><br \/>\n<span style=\"font-family: GulimChe;\">teach sed use or regexps to first-time users, but as a reference text<\/span><br \/>\n<span style=\"font-family: GulimChe;\">for those already acquainted with these tools.<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">QUOTING SYNTAX: The preceding examples use single quotes ('...')<\/span><br \/>\n<span style=\"font-family: GulimChe;\">instead of double quotes (\"...\") to enclose editing commands, since<\/span><br \/>\n<span style=\"font-family: GulimChe;\">sed is typically used on a Unix platform. Single quotes prevent the<\/span><br \/>\n<span style=\"font-family: GulimChe;\">Unix shell from intrepreting the dollar sign ($) and backquotes<\/span><br \/>\n<span style=\"font-family: GulimChe;\">(`...`), which are expanded by the shell if they are enclosed in<\/span><br \/>\n<span style=\"font-family: GulimChe;\">double quotes. Users of the \"csh\" shell and derivatives will also need<\/span><br \/>\n<span style=\"font-family: GulimChe;\">to quote the exclamation mark (!) with the backslash (i.e., \\!) to<\/span><br \/>\n<span style=\"font-family: GulimChe;\">properly run the examples listed above, even within single quotes.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">Versions of sed written for DOS invariably require double quotes<\/span><br \/>\n<span style=\"font-family: GulimChe;\">(\"...\") instead of single quotes to enclose editing commands.<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">USE OF '\\t' IN SED SCRIPTS: For clarity in documentation, we have used<\/span><br \/>\n<span style=\"font-family: GulimChe;\">the expression '\\t' to indicate a tab character (0x09) in the scripts.<\/span><br \/>\n<span style=\"font-family: GulimChe;\">However, most versions of sed do not recognize the '\\t' abbreviation,<\/span><br \/>\n<span style=\"font-family: GulimChe;\">so when typing these scripts from the command line, you should press<\/span><br \/>\n<span style=\"font-family: GulimChe;\">the TAB key instead. '\\t' is supported as a regular expression<\/span><br \/>\n<span style=\"font-family: GulimChe;\">metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">VERSIONS OF SED: Versions of sed do differ, and some slight syntax<\/span><br \/>\n<span style=\"font-family: GulimChe;\">variation is to be expected. In particular, most do not support the<\/span><br \/>\n<span style=\"font-family: GulimChe;\">use of labels (:name) or branch instructions (b,t) within editing<\/span><br \/>\n<span style=\"font-family: GulimChe;\">commands, except at the end of those commands. We have used the syntax<\/span><br \/>\n<span style=\"font-family: GulimChe;\">which will be portable to most users of sed, even though the popular<\/span><br \/>\n<span style=\"font-family: GulimChe;\">GNU versions of sed allow a more succinct syntax. When the reader sees<\/span><br \/>\n<span style=\"font-family: GulimChe;\">a fairly long command such as this:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed -e '\/AAA\/b' -e '\/BBB\/b' -e '\/CCC\/b' -e d<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">it is heartening to know that GNU sed will let you reduce it to:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed '\/AAA\/b;\/BBB\/b;\/CCC\/b;d'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # or even<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed '\/AAA\\|BBB\\|CCC\/b;d'<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">In addition, remember that while many versions of sed accept a command<\/span><br \/>\n<span style=\"font-family: GulimChe;\">like \"\/one\/ s\/RE1\/RE2\/\", some do NOT allow \"\/one\/! s\/RE1\/RE2\/\", which<\/span><br \/>\n<span style=\"font-family: GulimChe;\">contains space before the 's'. Omit the space when typing the command.<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to<\/span><br \/>\n<span style=\"font-family: GulimChe;\">large input files or slow processors or hard disks), substitution will<\/span><br \/>\n<span style=\"font-family: GulimChe;\">be executed more quickly if the \"find\" expression is specified before<\/span><br \/>\n<span style=\"font-family: GulimChe;\">giving the \"s\/...\/...\/\" instruction. Thus:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed 's\/foo\/bar\/g' filename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # standard replace command<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed '\/foo\/ s\/foo\/bar\/g' filename&nbsp;&nbsp; # executes more quickly<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed '\/foo\/ s\/\/bar\/g' filename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # shorthand sed syntax<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">On line selection or deletion in which you only need to output lines<\/span><br \/>\n<span style=\"font-family: GulimChe;\">from the first part of the file, a \"quit\" command (q) in the script<\/span><br \/>\n<span style=\"font-family: GulimChe;\">will drastically reduce processing time for large files. Thus:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed -n '45,50p' filename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # print line nos. 45-50 of a file<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;&nbsp; sed -n '51q;45,50p' filename&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # same, but executes much faster<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">If you have any additional scripts to contribute or if you find errors<\/span><br \/>\n<span style=\"font-family: GulimChe;\">in this document, please send e-mail to the compiler. Indicate the<\/span><br \/>\n<span style=\"font-family: GulimChe;\">version of sed you used, the operating system it was compiled for, and<\/span><br \/>\n<span style=\"font-family: GulimChe;\">the nature of the problem. To qualify as a one-liner, the command line<\/span><br \/>\n<span style=\"font-family: GulimChe;\">must be 65 characters or less. Various scripts in this file have been<\/span><br \/>\n<span style=\"font-family: GulimChe;\">written or contributed by:<\/span><br \/>\n<br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Al Aab&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # founder of \"seders\" list<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Edgar Allen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # various<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Yiorgos Adamopoulos&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # various<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Dale Dougherty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # author of \"sed &amp; awk\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Carlos Duarte&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # author of \"do it with sed\"<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Eric Pement&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # author of this document<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Ken Pizzini&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # author of GNU sed v3.02<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;S.G. Ravenhall&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # great de-html script<\/span><br \/>\n<span style=\"font-family: GulimChe;\">&nbsp;Greg Ubben&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # many contributions &amp; much help<\/span><br \/>\n<span style=\"font-family: GulimChe;\">-------------------------------------------------------------------------<\/span><br \/>\n<br \/>\n","protected":false},"excerpt":{"rendered":"<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dec. 29, 2005 Compiled by Eric Pement &#8211; pemente[at]northpark[dot]edu&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; version 5.5 Latest version of this file (in English) is usually at: &nbsp;&nbsp; http:\/\/sed.sourceforge.net\/sed1line.txt &nbsp;&nbsp; http:\/\/www.pement.org\/sed\/sed1line.txt This file will also available in other languages: &nbsp; Chinese&nbsp;&nbsp;&nbsp;&nbsp; &#8211; http:\/\/sed.sourceforge.net\/sed1line_zh-CN.html &nbsp; Czech&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8211; http:\/\/sed.sourceforge.net\/sed1line_cz.html &nbsp; Dutch&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8211; http:\/\/sed.sourceforge.net\/sed1line_nl.html &nbsp; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[39],"tags":[],"class_list":["post-362","post","type-post","status-publish","format-standard","hentry","category-os_linux_unix_macos"],"_links":{"self":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=362"}],"version-history":[{"count":0,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions"}],"wp:attachment":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}