William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

sed examples

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'`;

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).

[ "$#" != 1 -a "$#" != 2 ] &delimiter="\*\*\*\*"

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.

[ "$#" != 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

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
        /bin/grep -l $1 $i

One response to “sed examples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: