FAQ

On Nov 20, 4:58 pm, kra...@telus.net (John W. Krahn) wrote:
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- Hide quoted text -

- Show quoted text -
Hello again:
Thanks very much to John and Owen for helping me. I figured out I
didn't really need full-blown awk to do the awk-like thing I wanted to
do:
whenever a line from the infile has a particular string as the first
field, I need to know the last field. The unix awk command uses $NF
for
the last field. For my Perl script to do this, I needed to split each
element of the array that comes about from using <FILEIN>, and then
compare the first element to the string I am searching for, and if it
is a match, the last element of the array will be what I need. For an
input file:

field1 xxxx
QQQQQQ 2222
field3 3333
QQQQQQ 4444 wowwow


The script is:

#!/usr/bin/perl -wT
use CGI::Carp qw(fatalsToBrowser);
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!";
chmod 0755, '/tmp/out.txt' or warn "Cannot chmod '/tmp/out.txt': $!
\n!";

chomp(my @linesarray = <FILEIN>);

my @splitline;
foreach (@linesarray){
@splitline=split; #array is a split-up line from the
input file

if ($splitline[0] eq 'QQQQQQ'){
my $lastfield = $splitline[$#splitline];
print $q->center($q->h3("\$lastfield is $lastfield
\n"));
}
}

my $splitline=@splitline;

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

This script does a simple awk-like operation, but for powerful jobs
with multiple decisions it would be very difficult to write for an
unsophisticate like I am now. Thank you for the tips and help.
Mary

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 7 | next ›
Discussion Overview
groupbeginners-cgi @
categoriesperl
postedNov 20, '08 at 2:33p
activeNov 22, '08 at 3:13a
posts7
users3
websiteperl.org

3 users in discussion

Marys: 3 posts John W. Krahn: 2 posts Owen: 2 posts

People

Translate

site design / logo © 2021 Grokbase