------------------------------------------------------------------------------- Also see regex.hints for hints and tips of matching regular expressions ------------------------------------------------------------------------------- EXAMPLE: The best hint for a difficult script command is a good 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; } /^[ ]/{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' ------------------------------------------------------------------------------- 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; }' $new_file ------------------------------------------------------------------------------- Sed with arbatitry patterns >#!/bin/sh >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" problem with """ or "`" in patterns ???? ------------------------------------------------------------------------------ Delete between marker 1 and marker2 file ... MARKER 1 ... MARKER 2 ... then [] inclusive delete: $ sed