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

search text in file from STDIN 2

Status
Not open for further replies.

moonhead

Technical User
Apr 24, 2002
11
GB
hi there
i am currently writing a script that does stuff with files
one file has a list of numbers

i am trying to perform a match for a string entered from STDIN
it compares what the user enters to the content of the file
eg
enter a number...
2345 #user enters number

2345 is searched for in file

response 2345 was found
or
2345 was not found

i would be gratefull if someone could give me an example
many thanks



 
$file = "C:\\log.log";
chomp ($num = <STDIN>);
open (FILE, &quot;<$file&quot;) or die &quot;Can't open file: $! \n&quot;;
while (defined ($line = <FILE>)){
#if the file has the number
if ($line =~ /$num/i){
#print the file and path
print &quot;$file has the num $num\n&quot;;
last; # exit the while loop
}
}
 
Hi,
$input=<STDIN>;
chop ($input);
open (File, &quot;file.txt&quot;);
@file = <File>;
close (File);
$found = 0;
foreach $line(@file)
{
if ($input =~ /$line/)
{
$found = 1;
last;
}
else
{
next;
}
}

if ($found = 1)
{
print &quot;\n Found $input in file \n&quot;;
}
else
{
print &quot;\n Did not find $input \n&quot;;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top