The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
You need to properly quote variables with spaces or other special characters in them.

If you want to use Perl, it's rather weird to not do the actual matching in Perl also. Running the grep twice just because you want the number of matches first is also ... intriguing. Here, I cannot resist.

Code:
#!/usr/bin/perl
print "Enter a File name :";
chomp ($file = <STDIN>);
print "\n Searching file :";
if (-e $file)
{
    print "File Found\n";

    $lines = `wc -l < $file`;
    chomp $lines;

    print "Total number of lines in the file = $lines \n";

    print "Enter the pattern to search :";
    chomp ($pattern = <STDIN>);
    print "\n";
    # to search the no of words (pattern search)
    $abc=`grep "$pattern" $file`;
    $count = () = $abc =~ m/\n/g;
    print "Total number of results found: $count\n";
    print "here are the results ...\n$abc\n";
}
else{
    print "File not Found\n";
}
Reply With Quote