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: Beginner:
Grepping and collecting data

 



tbone587
User

Sep 29, 2011, 11:01 AM

Post #1 of 5 (756 views)
Grepping and collecting data Can't Post

I was wondering if there is a better/cleaner way of grepping data in linux via perl and then collecting information from that. I am currently using a system command to grep data and append it to a txt file. I then go through each line in the file to perform actions:


Code
system ("grep $PhoneNumber 00085D*.cfg >> numbermacs.txt"); 
system ("grep $PhoneNumber 00087b*.cfg >> numbermacs.txt");
system ("grep $PhoneNumber Polycom-user-0004f2*.cfg >> numbermacs.txt");
system ("grep $PhoneNumber *.xml >> numbermacs.txt");

open MACNUMS, "$SearchDir/numbermacs.txt" or die "Can't open $SearchDir/numbermacs.txt : $!";
my @Mac_Num_List = <MACNUMS>;
close (MACNUMS);

unlink ("$SearchDir/numbermacs.txt");



FishMonger
Veteran / Moderator

Sep 30, 2011, 7:36 AM

Post #2 of 5 (748 views)
Re: [tbone587] Grepping and collecting data [In reply to] Can't Post

Instead of the system call, you could run the command via `` backticks or the qx() operator to capture the output of the grep command.

Or, you could open a filehandle on each of the files and use Perl's grep command on the filehandle or process each file line-by-line.


tbone587
User

Sep 30, 2011, 8:56 PM

Post #3 of 5 (740 views)
Re: [FishMonger] Grepping and collecting data [In reply to] Can't Post

Do you have any example of the backticks or qx()command that would be used to capture the ouput on linux? And is this going to be faster than the method I used, or is there no advantage.


FishMonger
Veteran / Moderator

Oct 2, 2011, 9:36 AM

Post #4 of 5 (730 views)
Re: [tbone587] Grepping and collecting data [In reply to] Can't Post

Disk I/O is typically one of the slowest parts of a script, so reducing I/O will make the script more efficient.

Here's an example of using backticks to capture the info and place it into an array rather than a file.


Code
my @Mac_Num_List; 
push @Mac_Num_List, `grep $PhoneNumber 00085D*.cfg`;
push @Mac_Num_List, `grep $PhoneNumber 00087b*.cfg`;
push @Mac_Num_List, `grep $PhoneNumber Polycom-user-0004f2*.cfg`;
push @Mac_Num_List, `grep $PhoneNumber *.xml`;



tbone587
User

Oct 2, 2011, 4:34 PM

Post #5 of 5 (722 views)
Re: [FishMonger] Grepping and collecting data [In reply to] Can't Post

Thank you very much for that example. That is definately the road I was looking to take!

 
 


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

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