You could either create a regular expression to match both parts at the same time, or alternatively use the Perl
grep function to match the username and userid individually.
Using the following data file (phonebook.txt):
Jon:1:0123456789
Rob 1 0987654321
Sean 4 075312468
Tom-3-0111 222 333
Colin 3 0222 111 5555
You could write something like
#! /usr/bin/perl
my ($username,$userid) = @ARGV;
undef @ARGV;
my @phonebook = (<>);
@phonebook = grep /\b$username\b/ , @phonebook;
@phonebook = grep /\b$userid\b/ , @phonebook;
print foreach(@phonebook);
Then if you type
cat phonebook.txt | match.pl Jon 1
You will get
Jon:1:0123456789
Hope this helps,
JJ
Update:
I do realise that the above code still has bugs, I'm just trying to give our brother Monk some pointers. For a more robust solution, you should really define a file format (e.g. colon-sperated values), then you could use split to ensure you are only matching against the right fields.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.