Computing.Net > Forums > Unix > Search And Replace With Sed

Search And Replace With Sed

Reply to Message Icon

Original Message
Name: James Gordon
Date: September 18, 2002 at 23:55:14 Pacific
Subject: Search And Replace With Sed
OS: Sun Solaris
CPU/Ram: Unknown
Comment:

Hi,

I intend to do a search and replace for specific words for files under a directory (inclusive of subdirectories).

I know how to do that file by file using sed
and redirect it to the original file. But now, I need to search for multiple files and update accordingly.

Please help.


Thanks in advance.


Report Offensive Message For Removal


Response Number 1
Name: tom z.
Date: September 19, 2002 at 03:14:46 Pacific
Reply: (edit)

Maybe a for loop with a variable for your filenames generated by ls?

foreach file (`ls`)
do
whatever $file
done


Report Offensive Follow Up For Removal

Response Number 2
Name: David Perry
Date: September 19, 2002 at 05:31:38 Pacific
Reply: (edit)

for pattern in "string1" "string2" ; do
for file in `find /full/path/root/to/search/ -exec grep -l $pattern {} \;` ; do
ls -l $file ;
sed -e 's/$pattern/newpattern/' $file > $file.tmp
mv $file.tmp $file
done ;
done | mail user@domain.com


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Search And Replace With Sed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge