|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
thanks for the advice.
but how do i look for files that are in another directory from where sed was invoked? |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > how to use sed to search replace files throughout a directory tree |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|