Where should i look for this answer:


If you don't know the answer if you could please point me in the right direction
it would be appreciated. looking to search for more then 1 term at a time
with user interaction as shown in the script below. I have tried || but only
end up with everything being displayed. The current script displays the row
directly in front of the search term but I'm looking for it to display mutiple
search terms. Is this possible or does my entire script need to be re-written?

something along the lines of:

Search for: Name: || Number:


Search for: Name:, Number:

with the end results being something like:

John Wayne 225-3455-1234
Smith Berry 225-3143-2341

use Tie::File;
use Fcntl 'O_RDONLY';

$inputFile  = 'source.txt';
$outputFile = 'output.txt';

print 'Enter 1 to print to screen, 2 to print to a file: ';
chomp($outputOpt = <STDIN>); #chomp removes newline from input string
print 'Search for: ';
chomp(my $searchPattern = <STDIN>);

tie @lines, 'Tie::File', $inputFile, mode => O_RDONLY or die "Can't open $inputFile for reading: $!\n";

if (@results = grep(/$searchPattern/,@lines)){
    if ($outputOpt == 1){
        print join("This box will close in 10 seconds\n",@results);
    elsif ($outputOpt == 2){
        open(OUTPUT, ">$outputFile") or die "Can't open $outputFile for writing: $!\n";
        print OUTPUT join("\n",@results);
        print "Everything is in a file called $outputFile\n";

else {
    print "No match & this box will close in 10 seconds\n";

$num = 10;
8 Years
Discussion Span
Last Post by 115cruiser

Use ~=m/ ... , do not use grep.

so i should remove this line: if (@results = grep(/$searchPattern/,@lines)){
replace with ~=m/ ... ,

( i am reading up on ~=m/ ... , but am only finding info for m/ --- any suggestions


grep is OK in this situation because you have to search the entire file. grep uses "$_ =~ m/pattern/" internally to search for the pattern.

When you say search for multiple things in the file, how does the user input those multiple things? You have to tell the user something like:

Enter multiple search terms seperated by a space:

then your script gets the search terms and uses split() to create a list of those search terms and searches for them sequentially, or you could actually compile a regexp or a search pattern from the input after split()ing it into the serperate search terms.


that appears to work as i don't receive an error when seperated by a space. but I am not
pulling any data that way ---

when i do single searches the data pulls up fine. Any suggestions helpful ---

I have learned lots of new things about grep today

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.