How to search a string in a given line

S

super.sumanth

Hi,
i want to serach for a string in line read from a file,

open(fd,$file)
@buffer=<fd>;
foreach $line(@buffer)
{
#search for a string "Bin" in each line";
--------
}

How to do it??
Please help me.
Thanks
 
J

Josef Moellers

Hi,
i want to serach for a string in line read from a file,

open(fd,$file)

- lexical file handles are deprecated.
- use the 3-argument form of open.
- check the return value of open
- semicolon missing.
@buffer=<fd>;
foreach $line(@buffer)
{
if ($line =~ /Bin/) {
print "$line\n";
}
}

How to do it??
Please help me.
Thanks

You could do it in one go:

open (my $fd, '<', $file) or die "Cannot open $file: $!";
while (<$fd>) {
next unless /Bin/;
chomp;
# $_ contains "Bin"
}

HTH,

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top