Greg's Tech blog

sed (the stream editor)

Tuesday 07 of September, 2004

I've been using sed to modify the import files for the Exchange lab here are some tricks & links.

To match & replace a particular string:

"s/%Distribution Lists\/cn=[0-9][0-9][0-9][0-9]//ig" This matches "%Distribution Lists/cn=xxxx" where xxxx is four digits
s/ROSNT[0-9][0-9]/EAOEX1/gImatches ROSNTxx and replaces with EAOEX1
s/EWZNT[0-9][0-9]/EAOEX1/gImatches EWZNTxx
s/ou=ETS\//ou=ETSEXT\//gImatches ou=ETS\
s/o=ETS\//o=ETSEXT\//gImatches o=ETS\
s/ETS\//ETSEXT\//gImatches ETS\
s/p=ETS;/p=ETSEXT;/gImatches p=ETS;
s/o=ETS;/o=ETSEXT;/gImatches o=ETS;
s/ETS.ORG/etsext.ORG/gImatches ETS.ORG
sed -n -e "/\$:/p" <filename Prints only lines in filename containing "$:"
sed -e "/\$:/d" <filename Prints only lines not containing "$:"
sed -n -e "/^dn: CN=ets/,/^$/p" matches all the lines between one starting with ^dn: CN=ets and end a blank line (^$)

The general command line looks like:

sed -e "s/%Distribution Lists\/cn=[0-9][0-9][0-9][0-9]//ig" filename.
The quotes around the regex are required if there is a space in the regex. The regex can be added to a file and references with -f

sed -f sedscript.sed filename

  • The