sed examples
Blogs20102010-12-08
Today I found a very useful article - Sed - An Introduction and Tutorial which introduces usage of sed.
sed, not sad, makes people happy when process stream data in Linux. sed and awk are 2 killers in Linux. Perl’s Regular Express uses a lot from them.
I ever wrote a bash cgi script which uses awk to process stream. It is crazy to use bash to write cgi, but awk really proves its values in pipe stream processing.
For sed, the following are 2 common usages:
1. find all pdf files, remove "" from the begin to the end. $ find pdf/ -type f -name ”*.pdf” -exec ls -1 {} ; | sed -e “s/^/”/” -e “s/$/”/”
2. rename file, remove space. for i in *; do mv “$i” `echo $i | sed -e ‘s/s//g’`; done
I collected some previous scripts/snippets which use sed to make my daily routines easily and quickly:
1. add lines number for a file (not :set number in vi).
!/bin/ksh
[ ”$#” != 1 -a ”$#” != 2 ] &delimiter=”****” newfile=${2-$1.new}
nn=`egrep -n $delimiter $1|cut -d: -f1|sort +0nr -1|xargs|awk ‘{print $1, $2}’` startline=`echo $nn | cut -d’ ’ -f2` endline=`echo $nn | cut -d’ ’ -f1` startline=` expr $startline + 1` endline=` expr $endline - 1`
sed -n “$startline,$endline w $newfile” $1
2. add /* and */ to a range of lines: comment.
!/bin/bash
[ ”$#” != 3 ] &TMP=tmp$RANDOM sed -e “$1s/^//* /” -e “$2s/$/ *//” <$3 >$TMP mv $TMP $3
3. The following snippets mock egrep.
Usage() { echo “nThis program searches the string (such as all tty* in files) from all the files (including in all sub-dirs). Usage: $0 string path (default is current directory)n” exit 1 }
[ $# -eq 0 -o $# -ge 3 ] && Usage DIR=${2:-`pwd`}
for num in `find $DIR -type d -print` do cd $num echo “nDirectory $num:” name=`file * |/bin/grep text|/usr/bin/cut -f1 -d:` for i in $name do /bin/grep -l $1 $i done done
