[ale] need a simple shell script

Armsby John-G16665 John.Armsby at motorola.com
Tue Mar 15 09:43:18 EST 2005


Thanks guys.  You have been a real help.  And you are realy quick!

John


-----Original Message-----
From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of Fletch
Sent: Monday, March 14, 2005 10:46 PM
To: Atlanta Linux Enthusiasts
Subject: Re: [ale] need a simple shell script


>>>>> "Armsby" == Armsby John-G <Armsby> writes:

[...]

    Armsby> 1.  find *.html files (recurse) in a particular
    Armsby> folder. pipe it to: 2.  grep -i 'verified' pipe it to: 3.
    Armsby> grep 04 4.  Print the path and filename of the file which
    Armsby> meets criteria 1,2,3.

The oneliner run from zsh (looks under .; change to /foo/**/*.html(.) to look under /foo ):

perl -lne '$f++if/04/;$v++if/verified/i;$f&&$v&&print$ARGV;($f,$v)=()if eof' **/*.html(.)


Then there's the almost equivalent Ruby oneliner (also looks under .; uses Ruby's internal glob rather than the shell's):

ruby -e 'Dir.glob("**/*.html"){|n|File.open(n){|c|f,v=nil,nil;c.each{|l|f=1if l=~/04/;v=1if l=~/verified/i};puts n if f&&v}}'


And the longer pure perl:

#!/usr/bin/perl
use File::Find qw( find );
my @files;
find(sub{push @files, $File::Find::name if -f and /\.html/}, @ARGV);

for my $file ( @files ) {
  my( $has_04, $has_verfied );
  local( *CUR );

  unless( open( CUR, "<", $file ) ) {
    warn "Can't open '$file': $!\n";
    next;
  }

  while( <CUR> ) {
    $has_04 = 1 if /04/;
    $has_verified = 1 if /verified/i;
  }
  close( CUR );

  print $file if $has_04 and $has_verified;
}

exit 0;

__END__

-- 
Fletch                | "If you find my answers frightening,       __`'/|
fletch at phydeaux.org|  Vincent, you should cease askin'          \ o.O'
                      |  scary questions." -- Jules                =(___)=
                      |                                               U

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list