Perl formatting.

I have a file which has following data:

And I want the following out of it:

So basically I want only the first four command separated fields and strip off the rest. How do I do it in perl?
I want only perl code and no shell code.

I can split it and try to do something, but the incoming data can have any number of comma separated fields so was thinking of a more better solution.

Thanks!!

Try like...

perl -a -F, -n -e 'print join(q{,}, $F[0],$F[1],$F[2],$F[3]).qq{\n}' test.txt
$
$ cat f63
693t622, ZOMBIAC SERVICES, , 3267, N
985g367, DRUMBASH, , 2376, Y, 457
983y106, STEFFI, , 2344, N, 2345, B
$
$ # Using autosplit
$
$ perl -F, -plane '$_=join(",",@F[0..3])' f63
693t622, ZOMBIAC SERVICES, , 3267
985g367, DRUMBASH, , 2376
983y106, STEFFI, , 2344
$
$ # Using regular expression
$
$ perl -plne 's/([^,]+(,[^,]+){3}).*$/$1/' f63
693t622, ZOMBIAC SERVICES, , 3267
985g367, DRUMBASH, , 2376
983y106, STEFFI, , 2344
$
$

tyler_durden

2 Likes

Dosent seem to work with the following:

 
$ echo "60000005,MAT CON DEPT-DB,,1,N" | perl -plne 's/([^,]+(,[^,]+){3}).*$/$1/'
60000005,MAT CON DEPT-DB,,1,N

Try:

perl -plne 's/([^,]*(,[^,]*){3}).*/$1/'
1 Like

Thanks Scrutinizer. Could you please explain the logic behind it how it works?

I changed the + in durden tyler's suggestion from + (1 or more) to * (0 or more). It means keep 0 or more occurrences of any character but a comma ( [^,] ) followed by 3 times: the combination of a comma followed by 0 or more occurrences of any character but a comma . This is stored in back reference $1. Replace all this and the rest of the line (.*) by $1 which contains what was previously stored...

I changed the + in durden tyler's suggestion from + (1 or more) to * (0 or more). It means keep 0 or more occurrences of any character but a comma ( [^,] ) followed by 3 times: the combination of a comma followed by 0 or more occurrences of any character but a comma . This is stored in $1. Replace all this and the rest of the line (.*) by $1 which contains what was previously stored...