[Edinburgh-pm] perl autosplit

asmith9983 at gmail.com asmith9983 at gmail.com
Fri Jun 1 10:27:26 PDT 2007


I was trying to use autosplit to extract fields from a file with  the fields 
separated by "|" and suspect I may have found a bug, but it needs the review 
by experts like you folk on the list.

test.unl contains lines like
W361003|W|02/09/2006|02/09/2006|04/09/2006|36|1|16| |003| |80|Auto/|RXX|1|271.0|0.000000e+00|271.0|0.000000e+00|0.000000e+00|
W361005|W|02/09/2006|02/09/2006|04/09/2006|36|1|16| |005| |80|Auto/|RXX|1|186.0|0.000000e+00|186.0|0.000000e+00|0.000000e+00|

I want to extraxt the first field from each line.  I did it with cut and awk, 
then tried perl, but came a cropper with the following

head -2 test.unl |perl  -a -F '|' -ne '{print $F[0];};' which replied
Can't open perl script "|": No such file or directory

As the -a and -F options are linked  and I suspected option order might be a
misunderstanding on my part , so I tried
head -2 test.unl |perl    -ane '{print "$F[0]\n";};' -F'\|'
which worked correctly producing :-

W361003
W361005


  awk and cut did the job without  needing me to make much effort and less 
typing :-

head -2 test.unl |awk -F'|' '{print $1};'
head -2 test.unl |cut -d'|' -f 1

-- 
Andrew


More information about the Edinburgh-pm mailing list