Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

perl foreach

Posted on 2006-10-23
5
Medium Priority
?
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

618 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