Computing.Net > Forums > Unix > shell script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

shell script

Reply to Message Icon

Name: neelima_123
Date: December 20, 2006 at 04:07:18 Pacific
OS: UNIX
CPU/Ram: 2.26GHZ, 504MB of RAM
Comment:

Can any one send me a shell script to search and replace a string in a directory

neelima



Sponsored Link
Ads by Google

Response Number 1
Name: prakash_kharat
Date: December 20, 2006 at 04:52:06 Pacific
Reply:

Hi I'hv written for changing spaces to ":"...This might help you...If you mean to say that..you are searching string and Replacing Directory path.

# Change spaces to ":" so the tokens can be parsed.
DIRPATH=`echo $DIRPATH | sed -e 's; ;:;g'`



0

Response Number 2
Name: nails
Date: December 20, 2006 at 14:02:49 Pacific
Reply:

We were just talking about the gres script, global regular expression, at this link:

http://www.computing.net/unix/wwwbo...

Since it's sed it, doesn't change text in place. You have to redirect the output to a temporary file and then change the file name.


0

Response Number 3
Name: neelima_123
Date: December 20, 2006 at 21:18:50 Pacific
Reply:

Thanks for ur response,But I want to replace a sting in a file with another string

for ex :There is a directory "ABCD" which contain 50 files ,among these 50 files there are 20 files which conatin a particular string(lets say "hello world").I want to replace "hello world" with "hello something"...

Sorry for tat ambigious question.

neelima


0

Response Number 4
Name: neelima_123
Date: December 20, 2006 at 21:47:25 Pacific
Reply:

I forgot to specify that there can be any number of sub folders in that directory

neelima


0

Response Number 5
Name: nails
Date: December 21, 2006 at 09:11:18 Pacific
Reply:

Did you even bother reading the other thread? Place the following commands in a file called 'gres' and make it executable:

# D & R's gres
file="myfilename"
pattern=$1
replacement=$2
A="`echo | tr '\012' '\001' `"
sed -e "s$A$pattern$A$replacement$A" $file
# end gres

Execute it like this:

gres "hello world" "hello something"

If you want to seach a directory tree, use find:

find . -type f -print|while read myfile
do
echo "$myfile"
done

Or use the recursive option of ls:

ls -1R|while read myfile
do
if [[ -f $myfile ]]
then
echo "$myfile"
fi
done


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: shell script

Shell Scripting www.computing.net/answers/unix/shell-scripting/8475.html

Variables in Shell SCripting www.computing.net/answers/unix/variables-in-shell-scripting/6020.html

shell script to uninstall java www.computing.net/answers/unix/shell-script-to-uninstall-java-/5976.html