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:
grep file for lines contained in variable

 



regex2012
User

Oct 12, 2015, 7:51 AM

Post #1 of 5 (1049 views)
grep file for lines contained in variable Can't Post

I am trying to grep for a variable with patterns in a file.
So blist (a variable) contains this pattern:

Code
CORPORATE-EXPENSE-LIST 
CORPORATE-FUNDING-SCHEDULE
MARKETING-INFO-REPORTS
etc. there are about 20 lines.


I want to run a script to grep for each line above in the listfiles.txt.
So like this in bash one line statements:
cat listfiles.txt |grep CORPORATE-EXPENSE-LIST
Then the list is searched again:
cat listfiles.txt |grep CORPORATE-FUNDING-SCHEDULE





Code
open my $fh, '<', '/tmp/listfiles.txt' or die "unable to open file '$file' for reading : $!"; 
my @arr = <$fh>;
close $fh; # close
my @lines = grep /$blist/, @arr;
print @lines;


I am not able to make the search happen. Can someone give pointers?
The variable only prints out the patterns it contains but the grep doesn't work. So I just get:

Code
CORPORATE-EXPENSE-LIST 
CORPORATE-FUNDING-SCHEDULE
MARKETING-INFO-REPORTS
etc....



(This post was edited by regex2012 on Oct 12, 2015, 7:51 AM)


FishMonger
Veteran / Moderator

Oct 12, 2015, 8:34 AM

Post #2 of 5 (1044 views)
Re: [regex2012] grep file for lines contained in variable [In reply to] Can't Post

Use a regex with alternation.


Code
my $blist = 'CORPORATE-EXPENSE-LIST|CORPORATE-FUNDING-SCHEDULE|MARKETING-INFO-REPORTS'; 
my $file = '/tmp/listfiles.txt';
my @lines;

open my $fh, '<', $file or die "unable to open file '$file' for reading : $!";
while (<$fh>) {
push @lines, $_ if /$blist/;
}



regex2012
User

Oct 14, 2015, 7:23 AM

Post #3 of 5 (1037 views)
Re: [FishMonger] grep file for lines contained in variable [In reply to] Can't Post

Just tried it- works like a charm!

Great tip! I'll try it- thanks!


(This post was edited by regex2012 on Oct 14, 2015, 7:43 AM)


regex2012
User

Oct 15, 2015, 7:34 AM

Post #4 of 5 (1018 views)
Re: [FishMonger] grep file for lines contained in variable [In reply to] Can't Post

Fishmonger - you're feeding my appetite to learn more.
Why do we use the join | with the "|" ? Does that separate the lines using | as a delimiter?
Is the @words being fed this way to the variable $pattern?
Also, how is push used here? I read that push is used to add values to the end of the array. But I don't understand that in this context.
If you do have time to explain, that would really help.
The solution did work!


FishMonger
Veteran / Moderator

Oct 15, 2015, 8:22 AM

Post #5 of 5 (1015 views)
Re: [regex2012] grep file for lines contained in variable [In reply to] Can't Post

You should pickup a couple beginner level books.
Learning Perl, 6th Edition - http://shop.oreilly.com/product/0636920018452.do
Learning Perl Student Workbook, 2nd Edition - http://shop.oreilly.com/product/0636920018469.do

Additional recommendations:
Modern Perl - http://modernperlbooks.com/
Modern Perl Best Practices - http://shop.oreilly.com/product/110000790.do
Mastering Perl, 2nd Edition - http://shop.oreilly.com/product/0636920012702.do
Programming Perl, 4th Edition - http://shop.oreilly.com/product/9780596004927.do

https://www.perl.org/books/library.html

 
 


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

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