Solved

perl foreach

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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.…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now