[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

perl foreach

Posted on 2006-10-23
5
Medium Priority
?
497 Views
Last Modified: 2008-01-09
I have a file that contains a list of records, each record is seperated by a blank line. In another file I have a list of user names.
From the file that has the list of user names I need to take each user name and pull out the record for that user.

When I have just one user record to pull out I just use a simple perl oneliner like:

perl -n00e 'print if /^user1/' records.txt


I've tried a few ways to feed this the list of names from the user list but not getting that to work.


How can I get all the records for users.txt from the file records.txt by searching for the name in users.txt in the records file, also have to only search for records
that begin a line with the user name.

Ex:

users.txt
user1
user7
user9....

records.txt

user1: xxxxx
xxxxxx
xxxxx
xxxx

user2: xxxxx
xxxx
xxxx
xxxx


Thanks,

0
Comment
Question by:bt707
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 17788214
are u on unix or widnows??
0
 

Author Comment

by:bt707
ID: 17788226
on a unix box

Thanks,
0
 
LVL 16

Accepted Solution

by:
manav_mathur earned 2000 total points
ID: 17788388
you could use a while loop in ksh to read the line

while read line; do
perl -n00e 'print if /^'"$line"'/' records.txt
done < users.txt
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 17788417
or you could not use a one liner and create a script to read both files and then do what u require.

or u could try this

perl -n00e 'BEGIN{local@ARGV=(q#users.txt#);@u=<>}print if /^$f/ foreach$f(@u)' records.txt
0
 

Author Comment

by:bt707
ID: 17788478
Thanks manav, your while loop worked, haven't tried the other one. I had tried to use shell doing a for i in `cat users` then get pearl to read
the $i but that wasn't working at all.

This got what I needed to get.


Thanks,

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question