Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace a string in all files of a directory 3

Status
Not open for further replies.

RVSachin

Technical User
Nov 12, 2001
77
IN
Hi,

The following command appends the contents of myfil.txt to each *.scr file in each folder under the directory maindir.

Code:
find maindir -name '*.scr' | while read f; do cat /path/to/myfil.txt >> "$f"; done

My question is, how can we "find and replace a string" in all the *.SCR files in each subdirectory under MAINDIR?

For example if I have a main directory MAINDIR, and lot of sub-folders within MAINDIR which contain a *.scr file, then how do I find a particular line that has a string "IT'S ME" and replace it with "ARE U THERE" in that line.?

Thanks,

RV
 
perl -p -i.bak -e 's/oldstring/newstring/g'

That's off the top of my head, read the man page.
 
Previous version was too verbose.
Code:
ruby -i.bak -p -e 'gsub!(/foo/,"bar")' $f
 
find maindir -name '*.scr' | while read f
do echo "%s!IT'S ME!ARE U THERE!g\nwq" | ex -s "$f"
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PHV, It's seems like you have a solution for every problem.

Thanks to others as well.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top