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

Please give more clue
1) which parent folder ?
2) replace which filename ?
3) by which filename ?

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.

cd that dir first
grep -lE "myPattern" **/*.txt| xargs sed -in 's/myPattern/NEWTEXT/g'

will do the job.

1 Like

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...


kent$ echo "abc&nbsp123" | sed -r 's#\&nbsp#\&\|nbsp#g'
abc&|nbsp123

1 Like
sed 's/nbsp/|&/g' infile
# echo "abc&nbsp123"
abc&nbsp123
# echo "abc&nbsp123" | sed 's/nbsp/|&/'
abc&|nbsp123
#