[Omaha.pm] Another 30s script

Andy Lester andy at petdance.com
Fri Nov 18 16:49:48 PST 2005


On Nov 17, 2005, at 2:33 PM, Jay Hannah wrote:

> Task: Find any lines in a pipe-delimited file where the 3rd column
> contains a non-alphanumeric character.
>
> open (IN, "j.unl");
> while (<IN>) {
>     chomp;
>     @l = split /\|/;
>     print if ($l[2] =~ /[^a-z0-9]/i);
> }

What you're doing is very common, and so Perl has a number of tricks  
to handle most of that automatically.

Try this:

   perl -n -a -F'\|'  -e'print if $F[2] =~ /[^a-z0-9]/' j.unl

This has the benefit of being able to operate on multiple files, not  
just the one.  The -n says "loop through the input files."  The -a  
says "automatically split $_, the input line, into @F".  The -F says  
what to do the split() on.  The pipe in the -F parm has to be  
backslashed because the -F parm is always a regex.

See http://www.petdance.com/perl/command-line-options.pdf for more.



--
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance




More information about the Omaha-pm mailing list