Here's another way, basically a variant of what
roboticus posted (the key point being to use an array to keep the target words in one easily maintained place). This one uses a single capturing regex to handle all terms at once, and reports the hits instead of the misses:
use strict;
use warnings;
my @required = qw( name timestamp dl_retransmits_pct dl_throughput mac
+ );
my $req_regx = join( '|', @required );
while (my $line = <DATA>) {
my %matched = map{ $_ => undef } $line =~ /($req_regx)/g;
print $line if scalar( keys %matched ) == scalar( @required );
}
__DATA__
mac timestamp name dl_retransmits_pct dl_throughput is a hit
mac timestamp name dl_retransmits_pct throughout is a miss
name timestamp dl_retransmits_pct dl_throughput is a miss
foo mac name dl_throughput name timestamp name dl_retransmits_pct mac
+mac is a hit
name timestamp dl_retransmits_pct is a miss
dl_throughput is a miss
But as
Fletch suggested, if your input is really JSON data, you should use a JSON parser and handle the input as the data structure that it really is.
(updated to fix grammar and spelling)
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.