tophat has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perl Monks,
Ok full disclosure, I am completely new to scripting. I am essentially trying to recreate Excel's vlookup function. I have a file with a list of names, and I am trying to use these names to match them to their IP addresses on a different file.To be clear, I have 2 files, one with user names and another with user names and IP addresses. The goal is to use a name and return their IP address From what I do understand it's a pattern matching problem but my string will vary depending on the user name. Any ideas how I can structure this script are welcome! Thank you for reading.
Edit: Update
Thank you all. Even writing this response is helping me solve this problem To clarify, I am not using excel files. I mentioned it, because it seemed to be the quickest way to describe what I am trying to do. But I am working with two txt files that are lists. Here is the snippet of code that has been working for me so far, the variable $flag1 is the string I used to generate my list of user names.I added the line iterator and the "xx" so I have a pattern to match if I needed it. Essentially this snippet creates my list of users, or if you will, the first file. I now wish to use this output and match it against my second file of user and IP addresses.
What I can do: match and return values for a known and fixed string pattern (in this case the string "failed for user [],"
What I am having trouble with: using my output in the first file as a pattern for finding results in the second file. Is this a foreach situation? Or perhaps I should do a while loop?
my $linenum2=1; open FORMATLOG, "<$outputfile" or die "Cannot open $outputfile $!\n"; open (REPORT, ">$badreport") or die "Cannot open $badreport $!\n"; my @line =<FORMATLOG>; print "lines that matched $flag1\n;"; for (@line) { if ($_=~ m/failed for user (.*?),/) { print REPORT $linenum2++; print REPORT " :$1xx\n"; } } close FORMATLOG; close REPORT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use matched strings
by mikeraz (Friar) on Apr 12, 2011 at 16:49 UTC | |
|
Re: How to use matched strings
by toolic (Bishop) on Apr 12, 2011 at 17:03 UTC | |
by tophat (Initiate) on Apr 12, 2011 at 17:37 UTC | |
by mikeraz (Friar) on Apr 12, 2011 at 20:26 UTC | |
by tophat (Initiate) on Apr 12, 2011 at 23:52 UTC | |
|
Re: How to use matched strings
by young_david (Friar) on Apr 12, 2011 at 17:07 UTC | |
|
Re: How to use matched strings
by dj_nitrkl (Initiate) on Apr 12, 2011 at 18:48 UTC |