CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



harrry75
New User

Oct 20, 2008, 9:41 AM

Post #1 of 2 (1759 views)
Invoking Grep Command from Perl Script Can't Post

Hi,

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.

thanks.


KevinR
Veteran


Oct 20, 2008, 10:54 AM

Post #2 of 2 (1758 views)
Re: [harrry75] Invoking Grep Command from Perl Script [In reply to] Can't Post

perl solution:


Code
@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:


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

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


You can try that and see if it helps.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives