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.

Try:

perl -nle '$l[($.-1)%4].="$_ ";END{for ($i=0;$i<=$#l;$i++) {print $l[$i]}}' file

X is marked red.

1 Like

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? :wall:

Thanks for the response.

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.. :wink:

Woohoo, yes, I typed it on my linux konsole and I typed all those lower case l as 1. Thanks again :smiley: :smiley: :smiley:

Can you please explain this logic..

Thank you very much..
Tell me one thing is the array is automatically shifted when new value come between then.
Like suppose
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5

through code the new value is inserted to 2 index of array now it becomes

a[0]=1
a[1]=2
a[2]=12
a[3]=3
a[4]=4
a[5]=5

Is that correct...

No. That will never happen. The existing element is concatenated with a new string.
For e.g, if a[0] initially had 1 and you do this: a[0] .= 0 , then a[0] would now contain 10.

1 Like

[quote=balajesuri;302595767]
No. That will never happen. The existing element is concatenated with a new string.
For e.g, if a[0] initially had 1 and you do this: a[0] .= 0 , then a[0] would no

Thanks Guru