New Eyes on an awk script
Brad De Vries
devriesbj
Mon Nov 15 09:23:34 PST 2004
On Mon, 15 Nov 2004 06:01:23 -0800, Shawn Tayler
<stayler at xmtservices.net> wrote:
> Thanks Brad,
>
> Well this did change something. The syntax error now points to the '&&" in
> the if statements. According to my not so great SED & AWK book from
> O'Reilly, this is supposed to be a logical AND.
>
> Any suggestions?
>
> Shawn
>
> On Mon, 15 Nov 2004 08:18:56 -0500 Brad De Vries <devriesbj at gmail.com>
> exclaimed:
>
>
>
> > On Sun, 14 Nov 2004 11:15:37 -0800, Shawn Tayler
> > <stayler at xmtservices.net> wrote:
> > > Would one of you more knowledgable type show me the stupid mistake I am
> > > making with the following awk script? I have been banging my head
> > > against the "syntax error" wall for a while now.
> > >
> > > BEGIN { FS = "," }
> > > ++x
> > > if ( NF == 9 ) { print
> > > x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" } if ( NF == 10 )
> > > && ( $9 == $10 ) { print
> > > x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" } if ( NF == 10 )
> > > && ($9 != $10 ) { print
> > > x","$1","$2","$3","$4","$5","$6","$7","$8","$9","$10 } if ( NF == 8 ) {
> > > print x","$1","$2","$3","$4","$5","$6","$7","$8", , " }
> > >
> > > I keep getting a syntax error on the if statement.
> > >
> > > Shawn
> >
> > Two things Shawn; first, the block you want to execute must be
> > enclosed in braces and second, the entire condition for each "if" must
> > be enclosed in parenthesis. Try:
> > -------
> > BEGIN { FS = "," }
> > {
> > ++x
> > if ( NF == 9 ) { print
> > x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" }
> > if (( NF == 10 ) && ( $9 == $10 )) { print
> > x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" }
> > if (( NF == 10 ) && ($9 != $10 )) { print
> > x","$1","$2","$3","$4","$5","$6","$7","$8","$9","$10 }
> > if ( NF == 8 ) { print
> > x","$1","$2","$3","$4","$5","$6","$7","$8", , " }
> > }
> > -------
> >
> > Brad.
Strange, here's what I've got, how does it compare with what you've got:
-------
/home/bjd/tmp> cat it.awk
BEGIN { FS = "," }
{
++x
if ( NF == 9 ) { print
x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" }
if (( NF == 10 ) && ( $9 == $10 )) { print
x","$1","$2","$3","$4","$5","$6","$7","$8","$9",NA" }
if (( NF == 10 ) && ($9 != $10 )) { print
x","$1","$2","$3","$4","$5","$6","$7","$8","$9","$10 }
if ( NF == 8 ) { print x","$1","$2","$3","$4","$5","$6","$7","$8", , " }
}
/home/bjd/tmp> cat data
a,b,c,d,e,f,g,h,i
a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,i
a,b,c,d,e,f,g,h
/home/bjd/tmp> awk -f it.awk < data
1,a,b,c,d,e,f,g,h,i,NA
2,a,b,c,d,e,f,g,h,i,j
3,a,b,c,d,e,f,g,h,i,NA
4,a,b,c,d,e,f,g,h, ,
/home/bjd/tmp> rpm -qa | grep -i awk
gawk-3.1.1-9
-------
More information about the Linux-users
mailing list