UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old September 19th, 2004, 01:43 PM
unix101 unix101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 unix101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old September 19th, 2004, 11:21 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
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.

Reply With Quote
  #3  
Old September 20th, 2004, 12:28 AM
unix101 unix101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 unix101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for the advice.

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > how to use sed to search replace files throughout a directory tree


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway