nawk and if and END 
Author Message
 nawk and if and END

Hi,
I want to sum the numbers in Column one in a File.
Its easy, isn't it? But there are zeros in Col. 1, and they should be
changed to 1 for counting.
Therfore I have to use an IF Statement
nawk '{
        if ($1==0) {$1=1}     #if Col 1 eq. 0, add 1
       {total +=$1}                # count Col 1
        END {printf total" Blaetter sind gedruckt worden\n"} #at the end
there should be printed total
      }' pac_acct
 But I get the ERROR >>>> END <<<< illegal statement at ...

WY? What's wrong?
Thanks in advance
 Uwe Kohl
---------------------------------------------------------------------------

Dipl.-Ing. Uwe Kohl
FG Datenverarbeitung in der Konstruktion (DiK)
TU Darmstadt     Petersenstr.30  D-64287 Darmstadt
Tel: +49 6151 16 5484  Fax: +49 6151 16 6854

WWW: http://www.*-*-*.com/
---------------------------------------------------------------------------



Mon, 12 Aug 2002 03:00:00 GMT  
 nawk and if and END
Quote:

> Hi,
> I want to sum the numbers in Column one in a File.
> Its easy, isn't it? But there are zeros in Col. 1, and they should be
> changed to 1 for counting.
> Therfore I have to use an IF Statement
> nawk '{
>         if ($1==0) {$1=1}     #if Col 1 eq. 0, add 1

>        {total +=$1}                # count Col 1

         ^--remove this
Quote:
>         END {printf total" Blaetter sind gedruckt worden\n"} #at the end
> there should be printed total
>       }' pac_acct
>  But I get the ERROR >>>> END <<<< illegal statement at ...

> WY? What's wrong?

----
You have a syntax error at the indicated point. However, from an awk
style point of view, the following is more elegant:
    nawk '{
           $1==0 { $1 = 1 }
                 { total += $1 }
           END   { printf total " Blaetter sind gedruckt worden\n" }
          }' pac_acct

Unfortunately, I do not knoe enough German? to answer in that language;
if anything is unclear feel free to E-mail me.
--
Bob Stearns
University of Georgia

(706)542-5110



Mon, 12 Aug 2002 03:00:00 GMT  
 nawk and if and END

Quote:


> > Hi,
> > I want to sum the numbers in Column one in a File.
> > Its easy, isn't it? But there are zeros in Col. 1, and they should be
> > changed to 1 for counting.
> > Therfore I have to use an IF Statement
> > nawk '{
> >         if ($1==0) {$1=1}     #if Col 1 eq. 0, add 1

> >        {total +=$1}                # count Col 1
>          ^--remove this
> >         END {printf total" Blaetter sind gedruckt worden\n"} #at the end
> > there should be printed total
> >       }' pac_acct
> >  But I get the ERROR >>>> END <<<< illegal statement at ...

> > WY? What's wrong?
> ----
> You have a syntax error at the indicated point. However, from an awk
> style point of view, the following is more elegant:
>     nawk '{
>            $1==0 { $1 = 1 }
>                  { total += $1 }
>            END   { printf total " Blaetter sind gedruckt worden\n" }
>           }' pac_acct

> Unfortunately, I do not knoe enough German? to answer in that language;
> if anything is unclear feel free to E-mail me.
> --
> Bob Stearns
> University of Georgia

> (706)542-5110

wrong!

1.you cannot use an 'END' pattern as a part of an action statement.
  a correct awk script could be something like

BEGIN { action }   # execute at the beginning
pattern { action } # execute if pattern is true
{ action }         # execute always (empty pattern is true)
pattern            # print line if pattern is true {default action)
END { action }     # execute at end

2. use printf formatstring,expression,..
   or  print expression,expression,..

this should work [ not tested ]

nawk '
$1==0 { $1 = 1 }  # pattern { action }
{ total += $1 }   # { action }
END   { printf "%i Blaetter sind gedruckt worden\n",total }
' pac_acct

--
__________________________________________________________________

           o                     Eiso AB



Mon, 12 Aug 2002 03:00:00 GMT  
 nawk and if and END

Quote:


> > I want to sum the numbers in Column one in a File.
> > It's easy, isn't it? But there are zeros in Col. 1, and they should be
> > changed to 1 for counting.
> > Therfore I have to use an IF Statement
> > nawk '{
> >         if ($1==0) {$1=1}     #if Col 1 eq. 0, add 1

> >        {total +=$1}                # count Col 1
> >        ^--remove this
> >         END {printf total" Blaetter sind gedruckt worden\n"} #at the end
> > there should be printed total
> >       }' pac_acct
> >  But I get the ERROR >>>> END <<<< illegal statement at ...

> > WY? What's wrong?

> You have a syntax error at the indicated point. However, from an awk
> style point of view, the following is more elegant:
>     nawk '{
>            $1==0 { $1 = 1 }
>                  { total += $1 }
>            END   { printf total " Blaetter sind gedruckt worden\n" }
>           }' pac_acct

This is still wrong. The END rule is incorrectly embedded within
an outer read-a-record rule. Indeed, the whole script is incorrectly
surrounded by curly braces.

This works:

  nawk '$1 == 0 { $1 = 1 }
                { total += $1 }
        END     { print total " Blaetter sind gedruckt worden" }' pac_acct

But this is more succinct and reduces the number of assignments:

  nawk '{ total += $1 == 0 ? 1 : $1 }
        END { print total " Blaetter sind gedruckt worden" }' pac_acct

Pretty funky arithmetic, if y'ask me!

--
Jim Monty

Tempe, Arizona USA



Mon, 12 Aug 2002 03:00:00 GMT  
 nawk and if and END


% nawk '{
%         if ($1==0) {$1=1}     #if Col 1 eq. 0, add 1
%        {total +=$1}                # count Col 1
%         END {printf total" Blaetter sind gedruckt worden\n"} #at the end
% there should be printed total
%       }' pac_acct
%  But I get the ERROR >>>> END <<<< illegal statement at ...

An awk program consists of patterns and actions to take when the patterns
are true. Most patterns are evaluated once per input line, but there are
special patterns called BEGIN end END, which get executed at the start
and end of the script. If there is just an action, without a pattern,
it is evaluated for every input line. If there is just a pattern, without
an action, the input line is printed if the pattern evaluates to true.

OK, in your script, you have an action with no pattern. the pattern END
is not syntactically correct in an action. What you want is:

 nawk '$1==0 { $1 = 1 }       # treat 0 as if it were 1
       {total +=$1}           # sum Col 1
       # this is a bit simpler than using printf:
       END { print total, "Blaetter sind gedruckt worden"}
      ' pac_acct

You can ignore all my stupid little changes and just remove the outer set
of braces.
--

Patrick TJ McPhee
East York  Canada



Tue, 13 Aug 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. (start..end) where start > end

2. End to end examples?

3. US - BA, DESIGNERS NEEDED !! FRONT-END / BACK-END

4. CAD Engineer *** Front-End and Back-End Tool *** for Silicon Graphics, Mountain View, CA

5. US - BA, DESIGNERS NEEDED !! FRONT-END / BACK-END

6. read at end ... not at end, etc

7. Visual Basic front end and Cobol back end

8. end-of-record versus end-of-file?

9. web front-end, Python back end?

10. Browser front-end, python back-end

11. Passing shell variables to nawk snipet

12. Can any shed some light on this nawk problem

 

 
Powered by phpBB® Forum Software