Solved

perl foreach

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 92
Check file date before getting folder path 1 72
Perl string replace for refred url 9 62
add a syntax to a csv file 8 99
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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