#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep Power
    0

    how to use sed to search replace files throughout a directory tree


    Ok, i got most of my sed script working. i basically have two files that rely on each other, one file is the shell script, which calls sed to perform a sedscript, output to a file, and replace the original file (this needs to be done or sed does not commit the changes to the document!) the other file tells what sed is to do.

    so my shell script is:

    #Script name is shellscript
    sed -f sedscript filename.txt > tempfile
    mv tempfile filename.txt

    this script tells sed to read a script called sedscript, execute the commands it contains, output to a file named nice, and then rename/overwrite nice to the original filename. so basically, i would invoke the script by typing: shellscript

    now here is my sedscript:
    s/Apples/Oranges/g

    this says to replace the word Apples with Oranges.

    so here is my question.... i figured out how to create a script that search/replace MULTIPLE files with appropriate values, but i dunno how to make it search/replace for a file NOT in the directory where the sedscript is invoked. how can i make the script look for filename.txt in a directory that is one up/down/etc? i tried putting a simple cd.. into the script but then the sedscript wont be in a different directory so sed doesnt know what to do.

    also, i dont think i can use a find command because i dont want to replace filename.txt from other directories -- just directories from the current one on down.


    and no, i cant use perl because its not installed. thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,198
    Rep Power
    16
    this is very dangerous, you 'mv' without checking if sed worked
    Code:
    #Script name is shellscript
    sed -f sedscript filename.txt > tempfile
    mv tempfile filename.txt
    an error in sedscript will produce an empty 'tempfile'
    moved to 'filename.txt'

    to work on multiple files, you need a list of files

    find is a good tool for this, but save your original-files before
    renaming. the only probl is: sed gives no return status,
    something like:
    Code:
    #!/usr/bin/sh
    mkdir ${SAVED:=saved} || exit 1
    for files in `find . -type f`
    do sed -f sedfile $file >qqfile
        [ -s qqfile ] || continue # sed error, NO output
         cp $file $SAVED/`echo $file|sed 's/\//---/g'`
         cp qqfile $file
    done
    you can use 'mv' instead of 'cp', i don't know the OS you are on
    on older *nix 'mv' cannot 'mv' over filesystems, so is 'cp' saver.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    4
    Rep Power
    0
    thanks for the advice.

    but how do i look for files that are in another directory from where sed was invoked?

IMN logo majestic logo threadwatch logo seochat tools logo