------------------------------------------------------------------------------- Also see regex.hints for hints and tips of matching regular expressions And sed 1 liners http://sed.sourceforge.net/sed1line.txt Also examples from the gnu "info sed" ------------------------------------------------------------------------------ Print specific lines print first line of file (emulates "head -1") sed q print first 10 lines of file (emulates behavior of "head") sed 10q print the last line of a file (emulates "tail -1") sed '$!d' # method 1 sed -n '$p' # method 2 print the next-to-the-last line of a file sed -e '$!{h;d;}' -e x # for 1-line files, print blank line sed -e '1{$q;}' -e '$!{h;d;}' -e x # for 1-line files, print the line sed -e '1{$d;}' -e '$!{h;d;}' -e x # for 1-line files, print nothing print the last 2 lines of a file (emulates "tail -2") sed '$!N;$!D' print the last 10 lines of a file (emulates "tail") sed -e :a -e '$q;N;11,$D;ba' ------------------------------------------------------------------------------ Delete between Markers AAAA and BBBB Inclusive delete (delete start and end) sed '/AAAA/,/BBBB/d' Exclusive delete (output start and end) sed '1,/AAAA/p; /BBBB/,$p; d' or sed '/AAAA/,/BBBB/{ /AAAA/p; /BBBB/p; d }' delete end but output start sed '/AAAA/p; /AAAA/,/BBBB/d' or sed '/AAAA/,/BBBB/{ /AAAA/p; d }' delete start but output end sed '/AAAA/,/BBBB/{ /BBBB/p; d }' or sed '/BBBB/p; /AAAA/,/BBBB/d }' See next... ------------------------------------------------------------------------------ Print lines between markers AAAA to BBBB Including markers sed -n '/AAAA/,$p; /BBBB/q' file Excluding the end marker sed -n '/BBBB/q; /AAAA/,$p' file or sed -n '/AAAA/,$p; /BBBB/Q' file Excluding both markers sed -n '/BBBB/q; 1,/AAAA/d; p' file or sed '1,/AAAA/d; /BBBB/Q' file ------------------------------------------------------------------------------- Line numbers left aligned sed = file | sed 'N;s/\n/\t/' right aligned sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /' counting lines ("sed" only no "wc") sed -n '$=' file ------------------------------------------------------------------------------ Delete ALL blank lines sed '/^$/d' file Delete multiple blank lines (paragraph separators) sed '/^$/{ N; /^\n$/D; }' file or sed '/./,/^$/!d' file =============================================================================== Programming with sed.... ------------------------------------------------------------------------------ Delimiting "sed" Delimetors (also see next) Say you want to search for a path name that contains slashes `/' unfortunatally these are the sed regular expresion delimiters so you need to escape the slashes before you can search for it. This is not as easy as it sounds. old_path="/some/path/to/look/for" new_path="/some/path/to/look/for" old_path=`echo "$old_path" | sed 's/\//\\\\\//g'` new_path=`echo "$new_path" | sed 's/\//\\\\\//g'` sed 's'"$old_path"'/'"$new_path"'/g' $old_file > $new_file ------------------------------------------------------------------------------- Sed with arbatitry patterns #!/bin/sh # substitute {pattern} {replacement} # sed 's/'"$1"'/'"$2"'/g That's OK provided you know for certain that $1 will never contain a "/" or "\", and $2 will never contain a "/", "\" or "&". If $1 and $2 are completely arbitrary you need to do something like: pat=`echo "$1" | sed 's:[/\\]:\\&:g'` rep=`echo "$2" | sed 's:[/\\&]:\\&:g'` sed "s/$pat/$rep/g" Is there a problem with """ or "`" in patterns? -- seems not ------------------------------------------------------------------------------- Sed Programming Methology Example: This sed script grabs a 'face' file (which always starts with a space or tab) and repositions it correctly at the end of the mail header (denoted by a absolutely blank line). NOTE the use of lables. cat $FACE - $SIGNED | sed -n ' # first collect the new face at the start of the input 1 { s/^/X-Face:/; h; d; } /^[ \t]/{H;d;} # read the header; if X-face found just copy all : head /^X-Face:/ b body /^$/ b face p; n; b head # output a face at end of header :face x; p; g; p; n # just copy the rest of the file : body p; n; b body ' | /lib/sendmail "$@" ------------------------------------------------------------------------------- Adding a newline to sed (just add return -- messy) bourne shells: sed 's/;$/; }\ /' filename Csh family: sed 's/;$/; }\\ /' filename Of course you can always use a tr which is slower but neater sed 's/;$/; }@/' | tr '@' '\012' GNU sed allows you to use \r (for return) But uses \n on the input side. Do not use \n on output as that is 'null' sed 's/;$/; }\r/' In a similar way it allows the use of \t for tab. ------------------------------------------------------------------------------- Moving a line to the start of the file EG something1 this something2 ====> something1 this something2 something3 something3 sed -n ' /this/!H # if no match append to hold space /this/{x; H; } # found so exhange and append (prepend) ${g; s/\n//; p; }'