1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

global search and replace using sed

Discussion in 'Site & Server Administration' started by nevetS, Aug 17, 2005.

  1. #1
    because of what happened in this thread: http://forums.digitalpoint.com/showthread.php?t=24941

    I was forced to figure this out again.
    SEMrush
    basically, this is a shell script that will look for all .htm files and replace text within all of them.

    It's helpful if you have a lot of files and you just need to make a global change to your footer and you maybe didn't think about separating out your footer file or using dreamweaver templates, etc.

    This will not (I don't think) update text across multiple lines.
    
    #!/bin/bsh
    for file in $(find . -type f -name '*\.htm')
    do
    cat $file |sed "s/text-to-replace/replacementtext/g" > $file.new
    mv $file.new $file
    done
    
    PHP:
    AN IMPORTANT CAVEAT
    this will overwrite your file even if sed outputs an error - meaning you could potentially lose data if your sed command is not valid. Test your syntax before using this and backup your files.
     
    nevetS, Aug 17, 2005 IP
    SEMrush
  2. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    64
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use edit-files-in-place option with some extension to back up the file:

    -i[SUFFIX], --in-place[=SUFFIX]

    Not all versions of sed support this, though.

    J.D.
     
    J.D., Aug 17, 2005 IP
  3. Spartan_Strategy

    Spartan_Strategy Peon

    Messages:
    197
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    One of the best global text replacing engines I have ever seen is from abacre.com They have a free trial that can get you through about everything you would need for advanced text replacement. It also does the backup for you. http://www.abacre.com/afr/index.htm
     
    Spartan_Strategy, Oct 6, 2008 IP