#This will surpress all blank lines
cat testfile|sed -n "/^$/!p"
#Ditto
cat testfile|sed -e "/./!d"
cat testfile|sed -n "/./p"

#This gives the last match only
ls -t|sed -n -e "/\.c$/p"|sed -n "$ p"
#This gives the first match only
ls -t|sed -n "/\.c$/p"|sed -n "P;q"
#Note the way to exclude something
ls -t|sed -n -e /hcln.c/d -e "/\.c$/p"|sed -n "P;q"
#Note how to print a range
ls -t|sed -n "/\.c$/p"|sed -n "2;4p"

#This strips all the periods off the end of a line
sed "s/\.\{1,\}$//"
#Spaces
sed "s/ \{1,\}$//"

#This strips leading blanks
cat tmpfile|sed "s/^[[:blank:]]\{1,\}//"

#This shrinks all white space to one space
sed "s/[[:blank:]]\{2,\}/ /g"

#These are the character classes
[:alpha:]      letters
[:upper:]      upper-case letters
[:lower:]      lower-case letters
[:digit:]      decimal digits
[:xdigit:]     hexadecimal digits
[:alnum:]      letters or decimal digits
[:space:]      characters producing white-space in displayed text
[:print:]      printing characters
[:punct:]      punctuation characters
[:graph:]      characters with a visible representation
[:cntrl:]      control characters
[:blank:]      blank characters

# pad date to add anything missing using SCCS rules (usually hhmmss)
# check out the alternate length thing used with sh (${#date_len} is ksh)

date_len=`expr ${date} : '.*'`
junk=`echo 991231235959 | sed "s/.\{${date_len}\}//"`
date="${date}${junk}"
echo $date

#This would pad something with leading zeros using the same principal:

echo `echo 000 | sed "s/.\{${#1}\}$//"`$1
or, use:
typeset -Z3 mc=$1

This strips leading zeros:
echo $1|sed "s/^0\{1,\}//"
echo $1|sed "s/^.*\([^0].*\)/\1/"
as in:
echo ${1:+`echo $1|sed "s/^0\{1,\}//"`}

#This gives the first 3 digits of what is echoed into the pipe:
echo `echo 123456789 | sed "s/\(.\{3\}\).*/\1/"`

#This, everything byt the first 3 digits:
echo `echo 123456789 | sed "s/\(.\{3\}\)\(.*\)/\2/"`

#This is three examples of how to print a block of text
echo sed
cat tmpfile|sed -n '/^.aN$/,/^.aO$/p'
echo awk
cat tmpfile|awk '/^.aN$/,/^.a0$/{print $0}'
echo ed 
ed - tmpfile <<-!
/^.aN$/;/^.aO$/p
!

#This strips error messages
cat <x> |grep "\.aM"|sed s/....//|sed "s/\"//g"

#This appends a line after the 10th line
cat tmpfile |sed '10a\^J{line to add}'

#This is the mrchk junk 

        #sed -n "/${start}/,$ p"|awk '/ [oh][po][el][nd]/ {print $1}'
        #sed -n "/${start}/,$ p"|sed -n "/      open/p"|
        #sed -n "/${start}/,$ p"|sed -n "/      [oh][po][el][nd]/p"|
        #awk '{print $1}'`

#So, you wanna replace spaces with newlines using sed?
#Idiot - this is it... courtesy of BobD
cat x | sed 's/ /\^J/g'
#BACKSLASH control char

#Give it up... use tr instead (tr " " "\n").  The closest I've come is:
echo `cat tmpfile|sed 's/ /\\\\n/g'`
#but this also replaces the newlines with spaces.  DOH.

#This worked well - added the part to suppress blank lines... the "."
matches on everything except newlines...
cat tmpfile|tr " " "\n"|sed -n "/./p"

# This checks if $1 is in correct IP format
if [ `echo $1|sed -n "/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/p"` ]
then
        echo "it's a possible IP"
else
        echo "not"
fi

#A bizarre way to check if something is an odd number - actually works
#for negative numbers too
if [ $(echo `expr $line % 2`|sed -n "/1/p") ]
#Which is the equivalent of
if [ $(echo `expr $line % 2`|grep 1) ]
#or 
if [ `expr $line % 2` -eq 1 -o `expr $line % 2` -eq -1 ]

#Here sed is imitating ed, doing the substitution and saving the file
cat tmpfile|sed -n "s/10/ten/;w tmpfile"

#it has to come out of a stream otherwise the file is whacked, i.e.
#this doesn't work (writes blank tmpfile before it starts reading.)
sed -n 's/10/ten/g;w tmpfile' tmpfile
#also, it only works with sed commands that store changes to the pattern space
#meaning a,c, and i don't work like this because they write to standard
#out, not the pattern space.

#This appends line(s) and saves to original file
#file has to exist and not be zero length... this is too annoying, use ed.
cat tmpfile|sed -n '${a\
line \
line
}' >> tmpfile

#Here's how to find every directory on the machine that has src in it.
find . -type d -name src -print
#grep "/src"|sed "s/\(.*\/src\/\).*/\1/" |sort -u

echo "aaaabbbbcccc"|sed "s/bc//" 
aaaabbbccc
echo "aaaabbbbcccc"|sed "s/b*c//"
aaaaccc
elmer:/home/sbailey> echo "aaaabbbbcccc"|sed "s/*b*c//"
aaaabbbbcccc
elmer:/home/sbailey> echo "aaaabbbbcccc"|sed "s/b.*c//"
aaaa

#Date stuff

#Y2K fix...  input is YY/MM/DD  i.e. SCCS date
date=`echo $date|sed "s\([7-9][0-9]\)\(/[0-9]\{2\}/[0-9]\{2\}\)19\1\2"`
date=`echo $date|sed "s\([0-6][0-9]\)\(/[0-9]\{2\}/[0-9]\{2\}\)20\1\2"`

#flips YYYY/MM/DD to MM/DD/YYYY
sed  's/\([0-9]\{4\}\)\/\([0-9]\{2\}\)\/\([0-9]\{2\}\)/\2\/\3\/\1/'`

#Tellnav logic
pidline=`grep -n ${pidtime} $outfile|nawk -F ":" '{print $1}'`
#gets a list (or one numbers of lines that begin with the pidtime
#grep -n gives list like 1 2 3 - if only one, then top and bottom will
#be the same
top=`echo $pidline|awk '{print $1}'`
bottom=`echo $pidline|awk '{print $NF}'`
#This checks that the last line ends with ~~~
endline=`eval "sed -n '${bottom}p' $outfile|sed -n '/~~~$/p'"`
if [ -z "$endline" ]
then
	#if not, calculate the number of lines (=) until the next ~~~
	#and add them to the bottom.
	numlines=`eval "sed -n '${bottom};/~~~$/p' $outfile|sed -n '$='"`
	bottom=$(($bottom + ($numlines - 1)))
fi
eval "sed -n '${top};${bottom}p' $outfile"

# This works - a variable variable
count=22
var22="HIDY HO"
eval new='$'var${count}
name="wow"
eval "
sed '${count}a\\
$new $name
' tmpfile
"
