Home: Perl Programming Help: Intermediate:
Invoking Grep Command from Perl Script

New User

Oct 20, 2008, 9:41 AM

Views: 3789
Invoking Grep Command from Perl Script


I'm trying to get the line number of a particular search string from a log file using PERL.

I'm using grep -n to do that. But I'm getting into a problem because it contains a white space in it and the command starts considering the other words as name of the file and says the file name is not there.

this is the command which i'm trying to do in my script. where

$fileName = "orderList.txt";
$orderNumberSearchStr = "Added source_sys_id=18063bren3650908" ;

$temp = `grep -n $orderNumberSearchStr $fileName` ;

But this doesn't seems to work. Can someone help me on this.



Oct 20, 2008, 10:54 AM

Views: 3788
Re: [harrry75] Invoking Grep Command from Perl Script

perl solution:

@line_nums = (); 
open (my $file, 'orderList.txt') or die "$!";
while (<$file>) {
push @line_nums, $. if (/Added source_sys_id=18063bren3650908/);
close $file;

or maybe you can quote your grep arguments to use int he shell:

$fileName = "orderList.txt"; 
$orderNumberSearchStr = "'Added source_sys_id=18063bren3650908'" ;

$temp = `grep -n $orderNumberSearchStr $fileName`;

You can try that and see if it helps.