Operating System - HP-UX
1837195 Members
2346 Online
110115 Solutions
New Discussion

Re: Searching & Replacing string in a File

 
SOLVED
Go to solution
Sanjay Verma
Super Advisor

Searching & Replacing string in a File

Hi Friends,
I've hundreds of file in one directory and want to create a script which searches for a specific string "AB-00" and replace with "AB-11". Any suggestions? -Sanjay
Co-operation - The biggest chain reaction
12 REPLIES 12
Bhuvaneswari Selvaraj
Valued Contributor

Re: Searching & Replacing string in a File

for i in `ls`
do
rm ./sed_file 2>/dev/null
sed 's/AB-11/AB-11/' $i > ./sed_file
mv ./sed_file $i
done

this script will do... sed_file is a temp file. When you run this script ensure that it does not exist in your current dir
Bhuvaneswari Selvaraj
Valued Contributor
Solution

Re: Searching & Replacing string in a File

Sorry, I made a mistake while copying the script from my test machine to your reply..

The script must be like as shown in this..

for i in `ls dirname`
do
rm ./sed_file 2>/dev/null
sed 's/AB-00/AB-11/' $i > ./sed_file
mv ./sed_file $i
done

and make sure that the script itself is not in the ls output (save it a different location)
Bhuvaneswari Selvaraj
Valued Contributor

Re: Searching & Replacing string in a File

If it does not work, give the sed command in double quotes like

sed "s/$var1/$var2/" file1
Sanjay Verma
Super Advisor

Re: Searching & Replacing string in a File

Hi Bhuvaneswari,
Thanks for your reply. Some concerns are:
a) Why not put the script in the same directory?
b) What 'dirname' refers to?
c) Why it's required to create the temp file?? - Sanjay
Co-operation - The biggest chain reaction
Sanjay Verma
Super Advisor

Re: Searching & Replacing string in a File

Still having the issues like
- unable to run from other directory
- when run from the same directory it's changing the permission in the script too.
Can you suggest the way to correctly run this from other dir??
Co-operation - The biggest chain reaction
V.Tamilvanan
Honored Contributor

Re: Searching & Replacing string in a File

Hi,
Run this below script in your direcory where all your files need to be modified.

#!/usr/bin/ksh
for file in *
do
cp $file $file.bak
sed 's/AB-00/AB-11/g' $file.bak >$file
rm $file.bak
done
V.Tamilvanan
Honored Contributor

Re: Searching & Replacing string in a File

Hi,
Make sure you r not copying the script to the directory as it will change the scripts itself.
You just run those commands don't copy as a file.
Sanjay Verma
Super Advisor

Re: Searching & Replacing string in a File

Thanks Tamil. What about running these commands from a script from another dir??
Co-operation - The biggest chain reaction
Sanjay Verma
Super Advisor

Re: Searching & Replacing string in a File

It'll be great if I can run this script from another dir since it's changing the file continuously.
Co-operation - The biggest chain reaction
Bhuvaneswari Selvaraj
Valued Contributor

Re: Searching & Replacing string in a File

Hi,

Let me start from the beginning.. Assume that the files you want to change are in dir /temp/mydir

Copy the script that I had sent to you to /temp
Change the script, I mean the first line, ls dirname to ls /temp/mydir and execute the script.

Why it shud not be in the same dir is because, even your script will also get changed if it is in the same dir. hope this helps...
V.Tamilvanan
Honored Contributor

Re: Searching & Replacing string in a File

Yes. You can run this script from any directory.
You need to modify the scripts file search path with absolute path.
Suppose your files need to be modified are in a directory called /home/data. Then the script should be

#!/usr/bin/ksh
for file in /home/data/*
do
cp $file $file.bak
sed 's/AB-00/AB-11/g' $file.bak >$file
rm $file.bak
done


I hope this addressed all your concerns
Sanjay Verma
Super Advisor

Re: Searching & Replacing string in a File

Dear Bhuvaneswari & Tamil,
It all worked perfectly fine. What I did also is changed the script to .name and all ok now. Thanks & appreciate for you support & quick response.
Co-operation - The biggest chain reaction