On Nov 21, 4:47 pm, [EMAIL PROTECTED] (Owen) wrote: > On Fri, 21 Nov 2008 03:41:21 -0500 > > > > > > "michael spellman" <[EMAIL PROTECTED]> wrote: > > On Thu, Nov 20, 2008 at 8:32 PM, Owen <[EMAIL PROTECTED]> wrote: > > > > On Thu, 20 Nov 2008 06:32:51 -0800 (PST) > > > marys <[EMAIL PROTECTED]> wrote: > > > I am not altogether certain what you are trying to achieve. > > > > Read up on $. (See perldoc perlvar) That gives you the line number > > > that you are reading. > > > > Also I think you night be better off using a regex. > > > > if ($line =~ /xxxx/}{print "$. $line\n"}; > > > > This gives you the opportunity to get matches as well as pre and > > > post matches > > > > If you want to do awk type things, have a read of perldoc English > > Thank you for the advice. > > > I want to look in all lines in a many-line document, and if the line > > contains a particular string, like maybe 'QQQ', I want to take the > > next-to-last string from that line and assign a variable name $x to > > it. > > > A unix-like command to do the job on one line would be: > > my $x = ` awk '/QQQ/{ print $(NF-1) }' ` > > > Then if I had lines in the file like > > > QQQ 1 2 t horseradish 65 > > QQQ 24 65 18 > > rr QQQ wowmom 18 > > > I would get, after line#1 $x=horseradish > > and after line #2 $x=65 > > and after line #3 $x=wowmom > > > One thing I might be able to do is to pull in one line at a time into > > an array with the 'diamond operator' in the llama book and then > > somehow split on whitespace at each value of that array, put the > > resulting list into another array, and search this second array, > > position by position, for 'QQQ'. If any of the positions match, I > > could get the second-last word on that line from ($#array -1) somehow. > > But there are obvious problems here: for one thing, I need to > > surround the values with a quote, for example I need $x='horseradish' > > or else Perl will tell me it can't do the job. Plus I am not sure > > how to put the results of split into an array. But I am sure that's > > do-able. It's a learning experience for sure. > > > Thank you very much for the help. > > You need to run something like this. Adapt to your requirements > > ============================================================ > #!/usr/bin/perl -w > > use strict; > > while (<DATA>) { > my $line = $_; > if ( $line =~ /QQQ/ ) { > my @bits = split; > print "$bits[$#bits -1]\n"; > > } > > } > > __DATA__ > QQQ 1 2 t horseradish 65 > QQQ 24 65 18 > rr QQQ wowmom 18 > > ============================================================ > > horseradish > 65 > wowmom > > ============================================================- Hide quoted > text - > > - Show quoted text -
I guess I don't need awk for the job I described, only a lot of array reading and manipulating. If I have an input file: field1 xxxx QQQQQQ 2222 field3 3333 QQQQQQ 4444 hahaha and I want to find all lines containing the string QQQQQQ and then print out to a screen the last item on those lines, I've got to use the approach Owen wrote about above, where I first split and then successively test each field of each line to see if it matches, and if so I follow with a print of the last field: #!/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=>"myawk"); 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 [EMAIL PROTECTED]; print $q->center($q->h2(" awk program is finished!\n")); This gives a centered output at the browser: $lastfield is 2222 $lastfield is hahaha awk program is finished! This was a good learning experience, and the job is done, but it was a fairly simple task that took ~20 lines of code. I think there is a Perl resource somewhere that does awk jobs with one or two commands. Maybe a module. I want to thank Owen and John for their advice and all the pointers on syntax also. Mary -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/