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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing first line from a text file

Status
Not open for further replies.

tanveer91

Programmer
Nov 13, 2001
31
GB
Hi all,

I have a text file containing records of vehicles, i would like to display these records on the web browser. My problem is the first line of the file contains the system date, how can i display the details without including the date line??

e.g. text file:-

09.46 02 2002 April
FORD ,ESCORT ,1600,CL ,5DR,BLUE , 6.995,98,861649,S906FNWMPSAL
FORD ,ESCORT ,1600,CL ,5DR,DIAMOND , 6.495,98,863816,S244AUAM HBK
FORD ,ESCORT ,1600,CL ,5DR,RED , 5.495,98,859781,R543FCAMPEST
 
#!/usr/bin/perl -w

print "content-type: text/html\n\n";
open FILE, 'file';
while (<FILE>)
{
next if /09.46 02 2002 April/;
print;
}
close FILE;
exit(0);
 
Better:

#==== Code =====

use CGI;
my $j = new CGI;
print $j->header;

$\ = &quot;<br />\n&quot;; # makes it pretty on the browser.

open FILE, 'path/to/file';
<FILE>; #effectively tosses the first line...
print while(<FILE>);
close FILE;

#==== END CODE =====

This will skip the first line no matter what.
Johnny's code will work for that particular example, but change the date (happens all the time ;) and your seeing it in the output again.

--Jim
 
This is the easiest one (and most dirty)

print &quot;content-type: text/html\n\n&quot;;
$tmp = `awk 'NR==2,/[*]/ {print $0}' test.txt`;
print &quot;$tmp&quot;;

done.

It will remove the first line and then print the rest.
 
This is the correct one, did't try the code above, but is works fine :)

#!/usr/bin/perl
$tmp = `awk 'NR==2,/[*]/' test.txt`;
print &quot;$tmp&quot;;
 
This is the correct one, did't try the code above, but this code works fine

#!/usr/bin/perl
$tmp = `awk 'NR==2,/[*]/' test.txt`;
print &quot;$tmp&quot;;
 
Modifying yauncin's piece a bit, you could also do this:
Code:
while ( ($system, @data) = <FILE> ) { #add code }
#$system contains first line w/ the rest in @data
#and bypasses date problem
 
This will work as long as your timestamp keeps the same format that you displayed earlier:

#!/usr/bin/perl -w

use strict;

my @file = ();
open(FILE, &quot;./bar.txt&quot;) || die &quot;file i/o err: $!&quot;;
chomp(@file = (<FILE>));
close FILE;

foreach(@file){
print &quot;$_<br>\n&quot; unless $_ =~ m/^\d+/;
}
 
In Perl:
Code:
while(<>) {
    print unless $. == 1;
}
Or from the command line:
[tt]
perl -n -e 'print unless $. == 1' file
[/tt]
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top