Path: utzoo!attcan!uunet!olivea!bu.edu!dartvax!eleazar.dartmouth.edu!mjm From: mjm@eleazar.dartmouth.edu (Andy Behrens) Newsgroups: comp.unix.shell Subject: Re: awk question Message-ID: <1991Feb28.181115.27279@dartvax.dartmouth.edu> Date: 28 Feb 91 18:11:15 GMT References: <62322@eerie.acsu.Buffalo.EDU> Sender: news@dartvax.dartmouth.edu (The News Manager) Reply-To: andyb@coat.com (Andy Behrens) Organization: Burlington Coat Factory Lines: 34 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. > > 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? You should write 'prev' instead of '$prev'. Awk variables (unlike shell variables) are written without a dollar sign. The awk statements n="3" print $n # or print $(n) will print the third field of the input line. -- If you've got a hammer, If the only tool you have find a nail. is a hammer, you tend to see (George Bush, January 29, 1991) every problem as a nail. (Abraham Maslow) +-------------------+ | Andy Behrens | or: andyb%coat.com@dartmouth.edu | andyb@coat.com | bitnet: andyb%coat.com@dartcms1 +-------------------+ uucp: {uunet,rutgers}!dartvax!coat.com!andyb RFD #1, Box 116, East Thetford, Vt. 05043 (802) 649-1258 Burlington Coat, HC 61 Box 1B, Lebanon, N.H. 03766 (603) 448-5000