in reply to Re^3: parsing text file
in thread parsing text file
Sorry that I could not post the whole code
Here is the thing I've tried out.
Problem is that I'm unable to match the Numbers inside the data. Please suggest the best way to do this
#!/usr/bin/perl use strict; # Array to hold the text file data my @chunks; open(CONF, $txtFile) || die "cannot find the text file\n"; while(<CONF>){ chomp; if (/^#/){ # Must be a comment, skip it next; } elsif (/^\s*$/) { # Only contains whitespace, skip it # could be blank lines next; } elsif (/^M/){ # Contains dos/mac control characters my @lines = split /^M/, $_; for ( my $i = 0 ; $i <= $#lines ; $i++ ){ push(@chunks, $lines[$i]); } } else { # assumed to be a normal data line # trim trailing and leading spaces for parsing the data later $_ =~ s/^\s+|\s+$//g; push(@chunks, $_); } #print "Found data for ", scalar(@chunks), " lines in $csv\n\n"; } close(CONF); # Get the Array Index where the 'Summary of This Bill Period Charges' +text is located my $index = indexArray('Summary of This Bill Period Charges', @chunks) +; # skip next 4 lines $index = $index + 4; foreach ( $index .. @chunks ) { if ( $data =~m/(\d+)\s{2,}(\d+)\s{2,}((\d|-)?(\d|,)*\.?\d*)\s{2,}( +(\d|-)?(\d|,)*\.?\d*)\s{2,}/ ) { print $5; } } # Thanks to a post which gave me this snippet sub indexArray{ my ($text, @data) = @_; for( 1..@data ) { ; if ( $data[$_] =~ m/$text/ig ) { ; return $_-1; } } -1 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: parsing text file
by Sandy (Curate) on Jun 06, 2011 at 20:29 UTC |