FAQ
Hello:
Does anyone know how to use ‘awk’ in a script? 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’ , but for Perl the delimiters are slashes: $x = grep /
string/ line.
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’

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";


while ( defined(my $line=<FILEIN>) ){
chomp($line);
push (@infile,$line);
}

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

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

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

_______________________________________________________________________



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.

Thank you.
Mary

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 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