Results 1 to 3 of 3

Thread: runsed

  1. #1
    Will.Spencer's Avatar
    Will.Spencer is offline Retired
    Join Date
    Dec 2008
    Posts
    5,033
    Blog Entries
    1
    Thanks
    1,010
    Thanked 2,329 Times in 1,259 Posts

    runsed

    If you have a Unix server and shell access, you can make some pretty major changes to your web sites in just a few moments by using sed, PERL, or Awk.

    sed is the most simple of the three tools, but in one way it is too simple. If you use sed to edit a file, it sends the output to stdout (standard output) and you have to copy that output back into the original file.

    One solution is a quick little script called runsed. With runsed, you just put your sed commands into a file called sedscr and then execute the runsed command on the files you wish to edit.

    For example, let's say I was to change my AdSense publisher ID on a hundred of my web sites.

    First I would create the sedscr file with the sed commands I want to execute:

    Code:
    s/pub-4934305865481143/pub-43449505856524753/
    Then I would execute runsed on all of my include files:
    Code:
    [root@jupiter /usr/local/www/data]# runsed */includes/*shtml
    This isn't a tool for everyone, but it has saved me thousands of hours.


    Here's the source code to runsed:

    Code:
    # 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
    Submit Your Webmaster Related Sites to the NB Directory
    I swear, by my life and my love of it, that I will never live for the sake of another man, nor ask another man to live for mine.

  2. #2
    Shenron's Avatar
    Shenron is offline Administrator
    Join Date
    Dec 2008
    Location
    Portugal
    Posts
    1,900
    Blog Entries
    2
    Thanks
    561
    Thanked 548 Times in 368 Posts
    Awesome Will!

    Thx a lot. This will spare me hundreds of hours managing links!

    BTW... Would you be interested in changing you Pub ID to, let's say, a random number I'd tell you?

  3. #3
    Charles's Avatar
    Charles is offline Senior Net Builder
    Join Date
    Dec 2008
    Location
    On the Bike
    Posts
    1,211
    Blog Entries
    5
    Thanks
    311
    Thanked 311 Times in 212 Posts
    And, from experience working with Will...

    Code:
    s/pub-4934305865481143/pub-43449505856524753/g
    That will make the edit for all occurrences of "pub-4934305865481143" (replace globally), rather than just the first.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •