#!/usr/bin/perl -w # # csver.pl - A simple CSV delimited file viewer # Rob Hudson # URL: http://www.cogit8.org/rob/ # # Takes a CSV delimited file from the command line, and reads line by line and # displays each field on its own line. # # common usage: # csver.pl csv_file | less $|++; sub parse_csv { my $text = shift; my @new = (); push(@new, $+) while $text =~ m{ "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? | , }gx; push(@new,undef) if substr($text, -1, 1) eq ','; return @new; } my $file = $ARGV[0] if ($ARGV[0] ne '') || die "Must specify input file\n"; open FILE, $file || die "Can't open file: $file, $!\n"; my $record = 0; my $count = 0; my @columns = (); my $field = ''; while () { print "--(Record: " . $record++ . ")------------------------------------\n\n"; @columns = parse_csv($_); $count = 0; foreach $field (@columns) { print "Field " . sprintf("% 2s", $count++) . ": $field\n"; } print "\n"; } close (FILE); 0;