Perl parsing help required.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Perl parsing help required.
# 1  
Old 02-04-2012
Perl parsing help required.

Hello, I got a file like this.


5201
5202
5203
5204
1234
2345
3456
4567
6210
6220
6230
6240


The required output should be

5201 1234 6210
5202 2345 6220
5203 3456 6230
5204 4567 6240


I have huge files where patterns repeat in the same column,
like first x numbers of items would be column1, next x numbers be
column 2 etc. X varies with different files. Can I get an example code.

Thanks a ton.
# 2  
Old 02-04-2012
Try:
Code:
perl -nle '$l[($.-1)%4].="$_ ";END{for ($i=0;$i<=$#l;$i++) {print $l[$i]}}' file

X is marked red.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-04-2012
Thanks for responding Bartus11, I tried running your code, it gives me the following error.


$# is no longer supported at -e line 1.
Number found where operaor expected at -e line 1, near "$#1"


Now if I remove the # from the condition statement, there is no error, but the file remains the same, am I missing something here? Smilie

Thanks for the response.
# 4  
Old 02-04-2012
Did you copy and paste this code, or retyped it to your terminal? $#l - red is lower case "L", not number "1". Sorry for confusion.. Smilie
# 5  
Old 02-04-2012
Woohoo, yes, I typed it on my linux konsole and I typed all those lower case l as 1. Thanks again Smilie Smilie Smilie
# 6  
Old 02-04-2012
Quote:
Originally Posted by bartus11
Try:
Code:
perl -nle '$l[($.-1)%4].="$_ ";END{for ($i=0;$i<=$#l;$i++) {print $l[$i]}}' file

X is marked red.

Can you please explain this logic..
# 7  
Old 02-04-2012
$l[($.-1)%4].="$_ " => $. refers to line number. ($.-1)%4 would always be either 0, 1, 2 or 3. So, what's happening here is: Each line is appended to the elements of an array in index 0, 1, 2 & 3. ($x .= $y would mean concatenate $y to $x and store result it in $x, same as $x = $x . $y)

Here's what the array @l will contain in each run:
Run #: Elements in @l separated by comma
Run 1: 5201
Run 2: 5201, 5202
Run 3: 5201, 5202, 5203
Run 4: 5201, 5202, 5203, 5204
Run 5: 5201 1234, 5202, 5203, 5204
Run 6: 5201 1234, 5202 2345, 5203, 5204
Run 7: 5201 1234, 5202 2345, 5203 3456, 5204
Run 8: 5201 1234, 5202 2345, 5203 3456, 5204 4567
Run 9: 5201 1234 6210, 5202 2345, 5203 3456, 5204 4567
Run 10: 5201 1234 6210, 5202 2345 6220, 5203 3456, 5204 4567
Run 11: 5201 1234 6210, 5202 2345 6220, 5203 3456 6230, 5204 4567
Run 12: 5201 1234 6210, 5202 2345 6220, 5203 3456 6230, 5204 4567 6240

Finally, array @l contains 4 elements viz. "5201 1234 6210, 5202 2345 6220, 5203 3456 6230, 5204 4567 6240". Now printing each element in a line would give:
5201 1234 6210
5202 2345 6220
5203 3456 6230
5204 4567 6240
These 2 Users Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Xsltproc showing error in parsing xml...help required

I need to parse text between xml tags using xsltproc. It seems the easiest way. Here the Input file looks like <?xml version="1.0" ?> - <tag:ROOT xmlns:as="http://some.org/some.xsd" xmlns:tag="http://www.tag.org/schemas" xmlns:xs="http://some.org/"> - <tag:L1> - <tag:L2> - <tag:L3> ... (2 Replies)
Discussion started by: alpha_1
2 Replies

2. Shell Programming and Scripting

Perl script required for processing the data

I have following result.log file (always has 2 lines) which I need to process, cat result.log name.cmd.method,"result","abc","xyz"; name="hello,mine.12345,"&"tree"&" xyz "&" tree "&" xyz", data="way,"&" 1"&"rate-me"&"1"&"rate-me",str="",ret=""; now I need to extract the strings/data as... (4 Replies)
Discussion started by: perlDiva
4 Replies

3. Shell Programming and Scripting

PERL: Help required exact match

Hello, My requirement is to iterate over all the lines of a file and compare them with a word and perform some operations if exact match is found. For the snippet below, it works even if contents of line include "diff" and "diff:". I want it to work only if it is exactly "diff" and is not... (2 Replies)
Discussion started by: sarbjit
2 Replies

4. Shell Programming and Scripting

Help required in parsing a csv file

Hi Members, I am stuck with the following problem. Request your kind help I have an csv file which contains, 1 header record, data records and 1 footer record. Sample is as below Contents of cm_update_file_101010.csv -------------------------------------------------- ... (6 Replies)
Discussion started by: ramakanth_burra
6 Replies

5. Shell Programming and Scripting

Parsing information in perl

So i'm trying to write a perl script that logins into a network switch via ssh: #sh ip traffic IP statistics: Rcvd: 1460119147 total, 563943377 local destination 0 format errors, 0 checksum errors, 48401998 bad hop count 0 unknown protocol, 8379279 not a gateway ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

6. Shell Programming and Scripting

perl required output

Hi, I have a string in log file from that i need to pick the username. the string is like this-- pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid 2172 tid 3124: 160: 10110847: userName : pid... (5 Replies)
Discussion started by: namishtiwari
5 Replies

7. UNIX for Advanced & Expert Users

Perl - Help required

I am trying to upload a file to a SQL database table. The column type is IMAGE. I am looking for a solution to upload a word doc file. I tried 3 approaches. 1) my $fileToStore = "mytest.doc"; open IPFILE, "<", $name; binmode IPFILE; while (<IPFILE>) { $fileToStore .= $_; } close IPFILE;... (1 Reply)
Discussion started by: b.paramanatti
1 Replies

8. Shell Programming and Scripting

Perl Script required...

Hi All, Windows Platform. Perl Scripting. I have a file called 'hostnames.txt' contains hostname entries one by one line. Script has to perform the following command for each entry in that file. ovtopofix -G <hostname> Please anybody give me the script on this requirement. Thanks,... (1 Reply)
Discussion started by: ntgobinath
1 Replies

9. Shell Programming and Scripting

Perl parsing compared to Ksh parsing

#! /usr/local/bin/perl -w $ip = "$ARGV"; $rw = "$ARGV"; $snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw"; $snmpw = "/usr/local/bin/snmpwalk -Os -c $rw"; $syst=`$snmpg $ip system sysName sysObjectID`; sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies

10. Shell Programming and Scripting

perl help required

hi previously this program used to ask for user input to perform operation so i have done the modifications but problem is that i have to create new file and redirect that file as input to this file and i want to avoid that so i made following modifications in program and i am not sure if its... (1 Reply)
Discussion started by: zedex
1 Replies
Login or Register to Ask a Question