Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flat File - Ignore first line. 2

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I'm reading data fom a flat file and I want to ignore the first line as it contains the field names.

Here's what I have.

Code:
open(TBL,"file.txt") || die("Can't open file");
	my @TblRow=<TBL>;
	close(TBL);

        foreach my $records (@TblRow) {
	     chop $record;
		
             my @fields=split(/\|/,$records);
	     print "<strong>$fields[1]</strong> <br>";
		
         }


Thank you in advance.
 
Code:
open(TBL,"file.txt") || die("Can't open file");
    my $header_record = <TBL>;  # <-- Do with this as you will
    my @TblRow=<TBL>;
    close(TBL);

        foreach my $records (@TblRow) {
         chop $record;
        
             my @fields=split(/\|/,$records);
         print "<strong>$fields[1]</strong> <br>";
        
         }



Trojan.
 
Incidentally, slurping an entire file into an array isn't considered good practice. It's recommended that you read it line-by-line. Also note that, assuming you're trying to cut off newlines, the chomp function is better than chop:
Code:
open(TBL,"file.txt") || die("Can't open file");
my $header_record = <TBL>;  # <-- Do with this as you will

while( my $record = <TBL> ) {
   chomp $record;
        
   my @fields=split(/\|/,$record);
   print "<strong>$fields[1]</strong> <br>";
}
close(TBL);
 

Thank you ishnid for your input. Your tips are always appreciated.

But what if I want to sort by the first field ?

Thank you in advance.
 
i just managed to cobble this together...

Code:
awk -F\| '{if (NR==1) next; printf "%s%02d%s\n", "<strong>", $1, "</strong>"}' abc.txt | sort


Kind Regards
Duncan
 
... come on then, kick me in the teeth - i'm just starting to get to grips with awk and, although this is not the correct forum, i for one would be pleased to see a sed/awk solution from the command line that would do the job of a Perl script. any comments on the above? i.e. how to improve... is it the wrong way to go about this?, etc...


Kind Regards
Duncan
 
Can't you just
Code:
use NeolithicRecidivist::awk;
at the start of your program? [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top