Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!sdd.hp.com!ucsd!nosc!humu!pegasus!pilikia!art From: art@pilikia.pegasus.com (Art Neilson) Newsgroups: comp.unix.shell Subject: Re: awk question Message-ID: <1991Feb28.095620.21291@pilikia.pegasus.com> Date: 28 Feb 91 09:56:20 GMT References: <62322@eerie.acsu.Buffalo.EDU> Organization: Pilikia, Honolulu Lines: 38 In article <62322@eerie.acsu.Buffalo.EDU> haozhou@acsu.buffalo.edu (Hao Zhou) writes: >I am using awk to print some selected lines from a text file. What I >want to to is to hold off the current line until next line comes in >and a certain condition is satisfied before printing both lines. So I >need to store the current line in some variable as follows: > > prev = 'initialization' > awk '{{if (condition) \ > printf("%s \n %s \n", $prev, $0)} > {prev=$0}}' > >However the variable prev doesn't store the previous line. Instead the >printf prints out twice the current line. What am I doing wrong? > >Thanks for any help on this problem. > > > Hao > >-- >Internet:haozhou@acsu.buffalo.edu BITNET:haozhou%acsu.buffalo.edu@UBVM.BITNET >UUCP: rutgers!ub!haozhou All your curly braces are quite confusing. This looks like what you're trying to do. awk ' BEGIN { prev = 'initialization' } { if (condition) printf("%s \n %s \n", prev, $0) prev = $0 }' -- Arthur W. Neilson III | INET: art@pilikia.pegasus.com Bank of Hawaii Tech Support | UUCP: uunet!ucsd!nosc!pegasus!pilikia!art