in reply to Re: Grepping with Perl: How to stop after first match?
in thread Grepping with Perl: How to stop after first match?

Thanks for the suggestion, but as I'm not reading the input in a loop, I can't see a way to implement it.
My input is in the form of a stream consisting of many tar'ed text files written to standard-out when they're extracted by bzcat (from .tar.bz2 files).
I'm trying to use perl to do the grepping, but that's only a part of a larger shell script that looks something like this (snipped for clarity):
#! /bin/sh ## other stuff happens here, but snipped for clarity scdir=/var/log find $scdir -type f -name "nts_*.bz2" | # find interesting files in + $scdir perl -wnl -e '7 > -M and print;' | # ignore old files xargs bzcat -k | # unpack contents to STDOUT perl -wn00 -e ' # paragraph mode m[kernel\.hostname\s.\s(.*?)\n] and print "Hostname:\t$1\n"; # +grep for hostname m[\/bin\/date\n(.*?)\n] and print "Generated:\t$1\n"; # grep fo +r date ## I grep for other stuff here, but snipped for clarity m[Settings.for\s(.*?):\n] and print "Interface:\t$1\n"; # g +rep for eth interfaces m[Speed:\s(.*?)Mb\/s] and # grep for interface spee +d print "\ -speed:\t$1Mb\/s\n" ; m[Duplex:\s(.*?)\n] and # grep for interface duplex print "\ -duplex:\t$1\n" ; m[Auto-negotiation:\s(.*?)\n] and # grep for interface +autoneg print "\ -autoneg:\t$1\n" ; m[cpuinfo\nvendor_id\s+:\s(.*?)\n] and # grep for cpu id print "CPU:\t\t$1\n" ; ' exit $!

I'm a Perl novice, so am open to suggestions that I'm not approaching this the right way.

Replies are listed 'Best First'.
Re^3: Grepping with Perl: How to stop after first match?
by johngg (Canon) on Dec 07, 2007 at 12:11 UTC
    My first thought is, "no, you're probably not using the right tools for what you want to do."

    I would attempt the whole thing in Perl for starters. Have a look at opendir, readdir and closedir along with grep for finding the compressed tar archives you want to work with and placing the names in an array. Having found them I would then loop over them using CPAN modules to uncompress (Compress::Bzip2) and read (Archive::Tar) the archives. I would read each file in the archive into a string so that I could do as many or as few matches as I wanted.

    I should stress that I have never done anything like this before but I think you are more likely to meet with success by adopting this approach. I hope these thoughts will help you towards a solution but feel free to ask further if things aren't clear.

    Cheers,

    JohnGG