Uhmmm, the awk script you listed will only add the second fields
of lines 3 and 6, not the second fields of lines 3 *TO* 6.  Admittedly,
Shre's question was rather ambiguous.

You may need to change that to:

#!/bin/awk -f
 
NR == 3 {
  num1 = $2;
}
 
(NR > 3) && (NR <= 6) {
  num1 = $2 + num1;
}
 
NR > 6 {
  print num1;
  exit;
}

Or even better :) :), change that to:

#!/usr/bin/perl -w
 
while (<STDIN>) {
        $i++;
        next if $i < 3;
        last if $i > 6;
 
        $total += (split)[1];
}
print "$total\n";



Craig Southeren writes:
 > The AWK script below will do this. Save it into a file called "addnums",
 > make it executable with chmod +x, and run it with:
 > 
 > addnums datafn
 > 
 > where datafn in the name of the file with the data in it.
 > 
 > 
 > #!/bin/awk -f
 > 
 > NR == 3 {
 >   num1 = $2;
 > }
 > 
 > NR == 6 {
 >   print $2 + num1;
 >   exit;
 > }
 > 
 > 
 >     -----Original Message-----
 >     From: [EMAIL PROTECTED]
 > [mailto:[EMAIL PROTECTED]]On Behalf Of Shrestha
 >     Sent: Friday, 7 January 2000 15:07
 >     To: [EMAIL PROTECTED]
 >     Subject: [SLUG] addition using script file
 > 
 > 
 >     Suppose there is a file, which contains:
 > 
 >     a 12
 >     b 14
 >     g 9
 >     p 58
 >     d 25
 >     t 18
 >     w 32
 > 
 > 
 >     The two fields are seperated by space.
 >     If we have to add all the numbers of second field using script file, it
 > can be done using cut -d" " -f2 /file
 > 
 >     Can any one help to write a script file which adds the number from third
 > line to sixth line only.
 > 
 >     Any help will be highly appreciated!!
 > 
 >     Shre
 > 
--
SLUG - Sydney Linux Users Group Mailing List - http://www.slug.org.au
To unsubscribe send email to [EMAIL PROTECTED] with
unsubscribe in the text

--
SLUG - Sydney Linux Users Group Mailing List - http://www.slug.org.au
To unsubscribe send email to [EMAIL PROTECTED] with
unsubscribe in the text

Reply via email to