Re: awk command in script?

[email protected] ("John W. Krahn") Thu, 20 Nov 2008 13:58:20 -0800
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>
marys wrote:
> Hello:

Hello,

> Does anyone know how to use ‘awk’ in a script?

perl and awk have a lot of similar features so its usually preferable to 
use perl in a perl program instead of awk.

> It must have a
> different syntax than the unix analog, as does the ‘grep’ command.
> For grep, the syntax in the c-shell is:
> “grep ‘string’ ,

It's the same in every shell because grep is a standalone command.

man grep

> but for Perl the delimiters are slashes: $x = grep /
> string/ line.

That's because in perl grep is a built-in function.

> Maybe the same thing is going on with Perl.
> 
> I have searched the following sources with no help on awk:
> 
> perldoc  -f   ‘awk’
> ‘Beginning Perl’ by S. Cozen
> ‘CGI101’
> and the O’Reilly books:
> ‘Learning Perl’ aka the llama book
> ‘Intemediate Perl’
> ‘Advanced Perl’
> ‘CGI Programming with Perl’

man awk

> I have a file called /tmp/file.txt with one line:
> 
> field       xxxx
> 
> for grepping on xxxx, the script is:
> 
> 
> #!/usr/bin/perl -w
> use CGI::Carp qw(fatalsToBrowser);
> use CGI qw(:standard -no_xhtml);
> #use CGI ':standard';
> use strict;
> use diagnostics;
> my $q = new CGI;
> print $q->header;
> print $q->start_html(-title=>"mygrep");
> 
> 
> my @infile;
> my $q = new CGI;
> open (FILEIN, "/tmp/file.txt") or die "Can't open /tmp/file.txt for
> reading: $!\n!";
> open (FILEOUT, ">/tmp/out.txt") or die "Can't open /tmp/out.txt for
> writing: $!\n!";
> system "chmod 755 /tmp/out.txt";

perldoc -f chmod

chmod 0755 '/tmp/out.txt' or warn "Cannot chmod '/tmp/out.txt' $!";

> while ( defined(my $line=<FILEIN>) ){

In a while loop conditional defined() is implied for a readline.

>      chomp($line);
>      push (@infile,$line);
> }

Or more simply:

chomp( my @infile = <FILEIN> );


> my @zoom = grep(/xxxx/,@infile);          #looks for 'xxxx' in @infile

Why didn't you just test for /xxxx/ in the while loop, then you wouldn't 
need two arrays?


> foreach (@zoom){
>         print $q->center($q->h3("\nNext line containing 'xxxx' is:
> \n"),
>         $q->h3("$_\n"),
>         $q->h3("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _")  );
> }

Or print this as you found /xxxx/ in the while loop and you wouldn't 
need either array?


> print $q->center($q->h2(" grep program is finished!\n"));

grep is a built-in Perl funtion, not an external program.

perldoc -f grep


> The script works as it should for grep, but what if I want to output
> $NF (=xxxx) when a line has the string  ‘field’ in it?  There must be
> a way, but I can't find it.

What does $NF contain?  I would guess that you want the line number 
where /xxxx/ was found?  If so:

while ( my $line = <FILEIN> ) {
     next unless /xxxx/;
     print $q->center(
         $q->h3( "\nNext line containing 'xxxx' is:\n" ),
         $q->h3( $_ ),
         $q->h3( "At line number: $." ),
         $q->h3( '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _' )
         );
}




John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall