Operating System - Linux
1757026 Members
2004 Online
108858 Solutions
New Discussion юеВ

PERL help needed by newbie

 
exec22
Occasional Advisor

PERL help needed by newbie

After trying to figure this out on my own using the "camel book" over 4 hours and testing, I broke down. I am sure it is a very simple command/commands to accomplish what I am trying to do but I could not acomplish what I wanted to do.

Here is my problem:

I have few files on each of my servers with around a thousand lines in each, from which I need to make a simple search and replace. File format is simple:

ParameterName Value

where both ParameterName and Value are alphanumeric strings, e.g.,

FilesystemThreshold 92%
LargefileValue 9999999

what I need to do is to search for the ParameterName and replace the value to a preset string on the same line, but this line may repeat itself multiple times (repetition count varies from one file to the other) in this file and I have to do the replacement on every each occurance of the ParameterName.

What would be a simple one or two liner perl command to accomplish this ?

Thank you for your help.
5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: PERL help needed by newbie

The following thread, with me asking the question contains the perl code you need:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=830280

This one is also useful.
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=780662

Its not breaking down to ask a question. Its efficient use of your time.

Please do not hesitate to ask again.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
harry d brown jr
Honored Contributor

Re: PERL help needed by newbie


Why not just use "sed" ??

Here are two ways with sed:

(1) On the command line:

sed -e "s/\(ParameterName1\) \(.*\)/\1 MyAnswer/g" -e "s/\(ParameterName3\) \(.*\)/\1 YourAnswer/g" -e "s/\(ParameterName5\) \(.*\)/\1 TheAnswer/g" ./testparams > NEWFILENAME

(2) Create a file with the parameters that need changing:

vi testvalues
Add lines like this:
s/\(ParameterName1\) \(.*\)/\1 MyAnswer/g
s/\(ParameterName3\) \(.*\)/\1 YourAnswer/g
s/\(ParameterName5\) \(.*\)/\1 TheAnswer/g

sed -f testvalues ./testparams > NEWFILENAME

Here it is in action (without the redirect output to a new file):

[root@rndspt01 /tmp]# cat testparams
ParameterName1 hello
ParameterName1 test
ParameterName2 mom
ParameterName3 there
ParameterName4 why
ParameterName5 This is strange
ParameterName3 where
[root@rndspt01 /tmp]# sed -e "s/\(ParameterName1\) \(.*\)/\1 MyAnswer/g" -e "s/\(ParameterName3\) \(.*\)/\1 YourAnswer/g" -e "s/\(ParameterName5\) \(.*\)/\1 TheAnswer/g" ./testparams
ParameterName1 MyAnswer
ParameterName1 MyAnswer
ParameterName2 mom
ParameterName3 YourAnswer
ParameterName4 why
ParameterName5 TheAnswer
ParameterName3 YourAnswer

OR with the use of a parameter file:

[root@rndspt01 /tmp]# cat testvalues
s/\(ParameterName1\) \(.*\)/\1 MyAnswer/g
s/\(ParameterName3\) \(.*\)/\1 YourAnswer/g
s/\(ParameterName5\) \(.*\)/\1 TheAnswer/g
[root@rndspt01 /tmp]# sed -f ./testvalues ./testparams
ParameterName1 MyAnswer
ParameterName1 MyAnswer
ParameterName2 mom
ParameterName3 YourAnswer
ParameterName4 why
ParameterName5 TheAnswer
ParameterName3 YourAnswer
[root@rndspt01 /tmp]#


live free or die
harry d brown jr
Live Free or Die
Rodney Hills
Honored Contributor

Re: PERL help needed by newbie

Run the following one line on each server-

perl -pe 'BEGIN{$p=shift ; $v=shift ;}{s/ .*/ $v/ if /^$p /o}' ParameterName Value file1 file2 ... filen

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: PERL help needed by newbie

Forgot the '-i' option

perl -pi -e 'BEGIN{$p=shift ; $v=shift ;}{s/ .*/ $v/ if /^$p /o}' ParameterName Value file1 file2 ... filen

-- Rod Hills
There be dragons...
exec22
Occasional Advisor

Re: PERL help needed by newbie

thanks