[ale] awk masters...
Jeff Rose
jojerose at mindspring.com
Mon Jul 7 20:38:31 EDT 2003
aahh how did I know someone would offer a Perl alternative;)
Thanks, I will give it a shot when I get back to work. I tried single
quotes, double quotes, no quotes but not single within double quotes.
On Mon, 2003-07-07 at 18:49, Fletch wrote:
> >>>>> "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.
>
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list