If I have a bunch of tar.gz files, and I know these all contain just log file data, and I just want to find and list particular lines that match a given regex pattern -- that is, I don't care which log file(s) are involved, I just want the matching lines -- I would either use a shell command like this:
gunzip -c *.tgz | grep target_regex > target.hits
(You might need an extra option, "--binary-files=text" on the grep command, depending on which version of grep you have.)
Or else I'd write a perl script like this:
#!/usr/bin/perl
use strict;
use PerlIO:gzip;
for my $file (<*.tgz>) {
open( T, "<:gzip", $file ) or do {
warn "open failed on $file: $!\n";
next;
};
while (<T>) {
print if /target_regex/;
}
}
(And in the latter case, I'd probably use @ARGV to set the path for where to find the tgz files, what regex to apply, and/or whether to read the list of tgz file names from STDIN, so that the script serves a general range of uses.)
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.