Solved

perl foreach

Posted on 2006-10-23
5
486 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 129
Need to install DBD::MySQL on a MAC without a local MySQL server for Perl 4 68
stftime format 4 56
Vb script to unzip a files and rename the files 12 113
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

792 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