Site Statistics
 
Threads: 4,028
Posts: 17,593
Members: 2,997
Users Online: 16
Newest Member: preetha


Go Back   PC101 > Computer Related Forums > Software > UNIX / Linux

UNIX / Linux Linux is the UNIX-like operating system that provides personal computer users a FREE or very low-cost operating system.


Reply
 
LinkBack Thread Tools Display Modes
Old 06-06-2006, 09:10 AM   #1
Freshman
 
Join Date: Jun 2006
Posts: 1
Rep Power: 0 D.Juan is on a distinguished road
The Unix sed command

I am having trouble getting the Unix sed command to work. I want to replace one string with another in a bunch of files. This should be easy, but I am having trouble making it work.

Can someone please post an example for me?
D.Juan is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-06-2006, 02:29 PM   #2
Professor
 
Join Date: Jul 2005
Posts: 2,208
Rep Power: 6 Will.Spencer is on a distinguished road
sed (the Unix stream editor) has saved me hundreds and possibly thousands of hours in maintaining this web site.

Using the sed command can be more than a bit annoying, because of the need for temporary files. You edit file X and put the output into file Y and then you move file Y on top of file X. There are just too man opportunities for error.

Instead, I highly recommend a small script called runsed. Here is an example of runsed in action:
Code:
$ cat girlsnames.txt 
Diane
Laura
Barbara

$ cat sedscr
s/Barbara/Michelle/

$ runsed girlsnames.txt 
runsed: editing girlsnames.txt: 
runsed: done with girlsnames.txt
runsed: all done

$ cat girlsnames.txt 
Diane
Laura
Michelle
As you can see from the example, you put the sed commands you want to execute in the file called "sedscr" and then you use the runsed command on the file(s) you want to change. All temporary file handling is done automatically for you.

runsed is even smart enough not to edit its own sedscr file.

runsed is a small script. Here is a copy of the whole thing:
Code:
#! /bin/sh
# runsed - run a sed script on file(s), replace originals with edited versions

# Script looks nicer with tabstops set at 4 (in vi, :se ts=4)

myname="`basename $0`"
script=sedscr
stat=1                  # Default exit status (reset to 0 before normal exit)
temperr=/tmp/RUNSEDe$$
tempout=/tmp/RUNSEDo$$
trap 'rm -f $temperr $tempout; exit $stat' 0
trap 'echo "$myname: Interrupt!  Cleaning up..." 1>&2' 1 2 15

if [ ! -r $script -o ! -f $script ] ; then
        echo "$myname quitting: $script isn't readable or isn't a file." 1>&2
        exit
fi

case $# in
0) echo "Usage: $myname file-to-edit [files-to-edit]" 1>&2; exit ;;
esac

for x
do
        echo "$myname: editing $x: " 1>&2
        if [ "$x" = $script ] ; then
                echo "$myname: not editing $script!" 1>&2
        elif [ ! -s "$x" -o ! -f "$x" ]; then 
                echo "$myname: original $x is empty or not a file." 1>&2
        elif [ ! -w "$x" ] ; then
                echo "$myname: can't write $x -- skipping..." 1>&2
        else
                # If get here, run sed.  To keep source file's permissions
                # and owner the same, don't overwrite it until the end --
                # and use "cat > file" to write the file in place.
                sed -f $script "$x" >$tempout 2>$temperr
                if [ $? -ne 0 -o -s $temperr ] ; then
                        cat $temperr 1>&2
                        echo "$myname quitting: 'sed -f $script $x' bombed!?!" 1>&2
                        exit
                elif [ -s $tempout ] ; then
                        if cmp -s "$x" $tempout; then
                                echo "$myname: $x file not changed." 1>&2
                        else
                                if /bin/cat $tempout > $x
                                then echo "$myname: done with $x" 1>&2
                                else
                                        echo "$myname quitting: problem replacing $x?" 1>&2
                                        exit
                                fi
                        fi
                else 
                        echo "$myname quitting: sed produced an empty file - check your $script." 1>&2
                        echo "$myname: didn't change $x" 1>&2
                        exit
                fi
        fi
done

echo "$myname: all done" 1>&2
stat=0
exit
runsed is one of the many useful utilities in Unix Power Tools.

Will.Spencer is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exchange 2007: Get used to the command line Lyte PC & Tech Related News, Events and More! 0 01-07-2007 04:59 PM
What is NCQ (Native Command Queuing)? Varun Krishnan Hardware 0 09-17-2006 10:37 PM
US Marshals Switching to Red Hat Linux Lyte UNIX / Linux 6 09-09-2006 08:39 PM
Hacking SUSE Linux 10.1 OulZac UNIX / Linux 7 08-26-2006 03:48 PM
Unix Frequently Asked Questions Will.Spencer UNIX / Linux 4 12-22-2005 10:06 AM



All times are GMT -5. The time now is 07:29 PM.

Powered by vBulletin Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC5