Search and Replace text in folders and Subfolders | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Search and Replace text in folders and Subfolders

Shell Programming and Scripting


Tags
folders and subfolders

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-18-2011
Asheesh Asheesh is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 2 April 2012, 7:19 AM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Search and Replace text in folders and Subfolders

Hi,

I need help in writing a script to search a particular text in multiple files present in folders and sub folders and replace it with another string which also has special characters like '&', '|', etc.. I know sed command will be used to replace the text but i'm not sure how to use it for different files and for files in subfolders..

Can someone help..

Thanks in Advance !!

~Asheesh
Sponsored Links
    #2  
Old 03-18-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,929
Thanks: 80
Thanked 624 Times in 596 Posts
Please give more clue
1) which parent folder ?
2) replace which filename ?
3) by which filename ?
Sponsored Links
    #3  
Old 03-18-2011
sk1418 sk1418 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 8 March 2012, 11:08 AM EST
Posts: 236
Thanks: 1
Thanked 55 Times in 54 Posts
since you didn't give much detailed description, I guess you want to do this, pls try:

under one directory, find all files (including the subdirs) containing a text pattern, then replace this pattern with your new text.

PHP Code:
cd that dir first 

Code:
grep -lE "myPattern" **/*.txt| xargs sed -in 's/myPattern/NEWTEXT/g'

will do the job.
The Following User Says Thank You to sk1418 For This Useful Post:
Asheesh (03-21-2011)
    #4  
Old 03-21-2011
Asheesh Asheesh is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 2 April 2012, 7:19 AM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
I have to replace &nbsp with &|nbsp and I am using the folloing command

sed -e "s/&nbsp/&|nbsp/g"

But this replace &nbsp with &nbsp|nbsp...what should i do to get the desired results...
Sponsored Links
    #5  
Old 03-21-2011
sk1418 sk1418 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 8 March 2012, 11:08 AM EST
Posts: 236
Thanks: 1
Thanked 55 Times in 54 Posts
bash code:
  1. kent$ echo "abc&nbsp123" | sed -r 's#\&nbsp#\&\|nbsp#g'
  2. abc&|nbsp123
The Following User Says Thank You to sk1418 For This Useful Post:
Asheesh (03-21-2011)
Sponsored Links
    #6  
Old 03-21-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 26 June 2014, 10:29 AM EDT
Location: France
Posts: 2,929
Thanks: 80
Thanked 624 Times in 596 Posts

Code:
sed 's/nbsp/|&/g' infile


Code:
# echo "abc&nbsp123"
abc&nbsp123
# echo "abc&nbsp123" | sed 's/nbsp/|&/'
abc&|nbsp123
#

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Copy folders and subfolders from unix to windows chssastry Windows & DOS: Issues & Discussions 3 12-17-2008 07:31 AM
find size of folders and its subfolders with the Owner details bsandeep_80 UNIX for Advanced & Expert Users 5 04-15-2008 03:03 AM
Script to Analyze folders and subfolders seacros Shell Programming and Scripting 7 03-04-2008 01:02 PM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 09:24 AM
How to create folders/subfolders using shellscript ram2s2001 Shell Programming and Scripting 2 04-05-2006 05:20 AM



All times are GMT -4. The time now is 05:41 AM.