#!/usr/bin/perl -w use strict; while (<DATA>) { chomp; # deletes line ending, # If this is a file: # Windows, Unix, Old Apple # <CR><LF>, <LF>, <CR> doesn't matter at all # Perl chomp() in text mode handles all that. # allows $zip to be used in future code # as index [3] without having to worry about # the line ending after the zip code... # assign name and city to index [0,2] respectively # using list slice # just split() upon the '|" character. # many dataBsase export formats use '|' as a field separator. # instead of a "," to make the parsing easier. # "John Smith, Sr.", is harder than FIRST|LAST|TITLE #Now to be sure, there are fancy CSV modules that can #handle this complexity, but that does not appear to be #the case required here (commas within quotes can mean #different than the ones outside of quotes). my ($name, $city) = (split /\|/, $_)[0,2]; print "$name",'|',$city,"\n"; } =prints NAME|CITY AAA|STAT BBB|PPOR CCC|TRET =cut __DATA__ NAME|AGE|CITY|ZIP AAA|23|STAT|60001 BBB|34|PPOR|12345 CCC|11|TRET|2345
In reply to Re: Better method to cut columns from delimited file
by Marshall
in thread Better method to cut columns from delimited file
by stevieb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |