Operating System - HP-UX
1827894 Members
1767 Online
109969 Solutions
New Discussion

Need perl script for this test data

 
kumar143
Occasional Contributor

Need perl script for this test data

sample 2 records: I have million records like this

test data:

Name Measure1 Measure2 Measure3 Measure4
aaa 67 345 987
bbb 678 445 567 123

delimter ( ,)

I need to have perl script which reads input file from the user

Am intersted to do validation for Measure2 and Measure4 only

i need to pass one input file saying Pick Measure2 and Measure4
and do the validation for Measure2 Checking for Non zero values, if any row contains Non zero it needs to redirect to one file saying particular Name is having Measure value non zero.

one more validation if Measure4 contains the value greater than 500 then it also redirect to another file saying particular Name is having Measure value greater than 500 .


Output:
Measure2.txt file should show

Name Measure2
aaa


Measure4.txt file should show

Name Measure4
aaa 987
2 REPLIES 2
Dennis Handly
Acclaimed Contributor

Re: Need perl script for this test data

You say the delimiter is a comma but the example has a space. My script assumes space.
Also, I assume that the title isn't in the file?
(And the aaa line is missing a measure.)
I'm not sure why "bbb" isn't in your Measure2.txt file?

You can use two awk scripts to extract the data, if you don't care about two passes:
awk '
$2 > 0 {print $1}
' input-file > Measure2.txt
And the other:
awk '
$5 > 500 {print $1, $5}
' input-file > Measure4.txt
Hein van den Heuvel
Honored Contributor

Re: Need perl script for this test data

As Dennis wonders... where are the 'commas'?
Could this be the forum software eating tabs and spaces?
Could the missing but supposed 0 for the aaa line be expunged due to the presentation?

PLEASE ATTACH actual sample niput as a .TXT file next time?

Try this:

$ awk '!$2 {print $1 > "measure2.tmp"} $4>500 { print $1,$4 > "measure4.tmp" } ' x

$ cat measure2.tmp
a3a
a7a

$ cat measure4.tmp
b2b 567
b6b 567

$ cat x
a1a 1 67 345 987
b2b 678 445 567 123
a3a 0 67 345 87
b4b 678 445 67 123
a5a 1 67 345 987
b6b 678 445 567 123
a7a 0 67 345 87
b8b 678 445 67 123

Enjoy,
Hein.

ps... In perl...

$ perl -e 'open M2,">m2.tmp"; open M4,">m4.tmp"; while (<>) {@F=split; if (!@F[1]){print M2 "@F[0]\n"}; if (@F[3]>500){print M4 "@F[0] @F[3]\n"}}' x

$ cat m4.tmp
b2b 567
b6b 567
$ cat m2.tmp
a3a
a7a