The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 08-27-2007
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
add every 'x' element

Code:
awk -v var=4 -f sample.awk inputfile
where sample.awk is
Code:
(NR % var ) == 0 { sum+=$0; cnt++}
END {print sum, cnt }
This adds every 4'th element is the list.

If you want the count starting from the first column in the list, change the sample.awk to
Code:
BEGIN { row_cnt=1 }
(NR % row_cnt ) == 0 { sum+=$0; cnt++; row_cnt+=var }
END {print sum, cnt }