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

Finding the first instance only of a number in a file

Status
Not open for further replies.

pcutler

Technical User
Jan 18, 2002
59
CA
Hi,

I have a file that is split into four reports. I'm using PERL to pull out lines that start with numbers like 301, 533 ect from each report.

Everything was working great, until I encountered a line that has 301 in it, but not at the begining.

How do I tell PERL to look for lines that BEGIN with 301, 533 etc and ignore instances where those numbers occur after the start of the line.

Thanks in advance for your help.

PC
 

The '^' character denotes the beginning of a line. Here is an example:
Code:
$match = 301;
if ($line =~ /^$match/) {
    print "found it \n";
}
 
my $abs_file = "/path/to/file";
open(IN, &quot;<$abs_file&quot;) || die &quot;Can't open $abs_file!&quot;
while(<IN>) {
if (/^\d\d\d/) {
### This is a good line - it starts with 3 digits
}
}

HTH. Hardy Merrill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top