1838767 Members
2991 Online
110130 Solutions
New Discussion

Re: Perl/Sed Question

 
SOLVED
Go to solution
Hunki
Super Advisor

Perl/Sed Question


Requirement : I have this application build procedure in which I need to manually modify the attached file in order for me to change the tags (Tg_SysTest_030607) and change it with a new tag.

I would need a script which propmts me for changing the tags and changes the tags in the following places in the attached file :

....
SAZ.corba.tag=Tg_SysTest_030607
...
SAZ.ejb.tag=Tg_SysTest_030607
...
SAZ.web.tag=Tg_SysTest_030607
...
SAZ.config.tag=Tg_SysTest_030607
...
SAZ.tag=Tg_SysTest_030607
...
JAZ.ejb.tag=Tg_SysTest_030607
...
JAZ.web.tag=Tg_SysTest_030607
...
JAZ.config.tag=Tg_SysTest_030607
...
JAZ.tag=Tg_SysTest_030607
...
LAZ.corba.tag=Tg_SysTest_030607
...
LAZ.ejb.tag=Tg_SysTest_030607
...
LAZ.web.tag=Tg_SysTest_030607
...
LAZ.config.tag=Tg_SysTest_030607
...
LAZ.tag=Tg_SysTest_030607
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Perl/Sed Question

Then I strongly suggest that you begin to write such a script. Laziness should never be rewarded. You should at least be able to start on such a script and then ask for ideas.
If it ain't broke, I can fix that.
Peter Godron
Honored Contributor

Re: Perl/Sed Question

Hunki,
can't you do a:
sed "1,$ s/Tg_SysTest_030607/Tg_SysTest_030707/g" build_0607.sh > build_0707.sh

or use a variable ($builddate) ?



Hunki
Super Advisor

Re: Perl/Sed Question

But the thing I might not be knowing the previous tag and want to change the entry for the places where "tag" is mentioned.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl/Sed Question

Hi:

This looks for the pattern "tag=" and substitutes "Tg_SysTest_999999" for whatever follows it.

# perl -pi.old -e 's{(.+tag=)(.+)}{\1Tg_SysTest_999999}' file

A backup copy of the original input file is made as "file.old" and your modified file left inplace.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Perl/Sed Question

The straw that broke the camel's back.

Hunki, Clay really has valuable advice here. Think about it! We have all seen you ask many questions here. It is (more than) time for you to revisit those question and ALL the answers, also the once which might not have seen pertinent, and learn from them

It is a huge disspointment to see you come back with what seems to be a trivial question at this point in time.

For the problem itself, I would just use a perl or sed one-liner, with at tight a match as you can find for the .tag= lines.
Something like:

perl -pe 's/^(\S+\.tag=).*/${1}my_test_tag/' old > new

So this instructs perl to look for lines starting with non-spaces and the string ".tag=" and to remember all that in $1
substitute that, and the rest (the old tag captured with .*) with the remembered strign and the new tag value.

Good luck, and do try some more yourself first!

[Oh, Go give yourself 5 points for providing a clear input data example! :-]

Cheers,
Hein.



Hunki
Super Advisor

Re: Perl/Sed Question

Thanks so much , one more question can u explain a little bit abt this perl command so I can try making one myself in future.

Thanks,
hunki
Hunki
Super Advisor

Re: Perl/Sed Question

thanks guys !