[ale] awk masters...

Fletch fletch at phydeaux.org
Mon Jul 7 18:49:57 EDT 2003


>>>>> "jojerose" == jojerose  <jojerose at mindspring.com> writes:

[...]

  jojerose> awk '{if(substr($1,7,2) == '$i') print $1}' recs.dat > newfile.dat

    jojerose> it's the $i that i can't get to work.  awk thinks it is
    jojerose> one of 'its' variables.  How can I get awk to recognize
    jojerose> it as a shell variable?

Well, you could either assign it to a variable:

awk -v i="$i" '{if(substr($1,7,2) == i){ print $1 }}' recs.dat


Or you'd need to have quotes around it when awk parses it:


awk '{if(substr($1,7,2) == "'$i'") print $1}' recs.dat 


Or just write it all in perl to begin with . . .

#!/usr/bin/perl -w
use strict;

my %codes;
open( CODES, "codes.dat" ) or die "codes.dat: $!\n";
while( <CODES> ) { chomp; $codes{ $_ }++ }
close( CODES );

open( RECS, "recs.dat" ) or die "recs.dat: $!\n";
while( <RECS> ) {
  my $cur = ( split(/\s+/, $_) )[0];
  print $cur if $codes{ substr( $cur, 6, 2) };
}
close( RECS );

exit( 0 );

__END__


Also more efficient that way since you only go over recs.dat once
rather than $( wc -l codes.dat ) times.


-- 
Fletch                | "If you find my answers frightening,       __`'/|
fletch at phydeaux.org   |  Vincent, you should cease askin'          \ o.O'
770 294-0820 (m)      |  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