Operating System - HP-UX
1833861 Members
2417 Online
110063 Solutions
New Discussion

Help me in shell scripting

 
SOLVED
Go to solution
Virumandi
Frequent Advisor

Help me in shell scripting

Hi .,

I want to grep a pattern in my file and want to replace it with a new one.

Here is the line where i need to modify

multimediaGroup type="media" dbid="525"

I want to replace the dbid value from 525 to 635 and how can i able to do it.

How can I write a script to read my file and grep the pattern 525 and get new input from me as 635 , then make it as modification .So finally I need that my file should have the entry as follows..

multimediaGroup type="media" dbid="635"

AnyBody well in scripting can help me..

Thanks & regards
Suseendran .A
4 REPLIES 4
Peter Godron
Honored Contributor

Re: Help me in shell scripting

Hi,
how about:
sed "1,$ s/525/635/g" input.dat > output.dat

Please note, this replace ALL occurances of 525 with 635.
Virumandi
Frequent Advisor

Re: Help me in shell scripting

No I dont want mu script to change all occurance

It want to tale bid number as a reference and to replace the ID value..

I got a Solution for the above by the following script

sed -e 's/multimediaGroup type="media" dbid="525"/multimediaGroup type="media" dbid="635"/' file >/tmp/tmpfile.$$

Thanks for the help...
Peter Godron
Honored Contributor
Solution

Re: Help me in shell scripting

Hi,
can you please close this thread, as you have already raised the follow-on ones ;-)

Rather than have multiple threads, it may be better to detail the actual problem and let people provide a better (complete) solution.
Doug O'Leary
Honored Contributor

Re: Help me in shell scripting

Hey;

perl -pli -e '/(multimediaGroup type="media" dbid=)"525"/\1"635"/g' ${file}

should do the trick. It'll update the file(s) in place so no need to move temporary files back into place.

One thing I will usually do is skip the i argument for the first run:

perl -pl -e '/(multimediaGroup type="media" dbid=)"525"/\1"635"/g' ${file} | grep 635

to verify *ONLY* the lines you're looking for are affected. If that looks good, add the -i argument and your files will get updated.

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html