awk with For loop

Tags
awk, for loop, loop, newbies

 
Thread Tools Search this Thread
# 1  
Old 1 Week Ago
awk with For loop

Hi

My Requirement is to take the sum of each column
below is the input file.
Code:
1 2 3 4
1 2 3 4
1 2 3 4

Initial i was using below command to achieve my desired result. however this was adding the row and not column.
i am not able understand why this is happening
Code:
 awk '{for(i=1;i<=NF;i++)x+=$i;print x;x=0}' M.txt  ----->1

After many hit&trial attempt and googling i am able to sum up the column . which is mentioned below.
Code:
 awk '{for(i=1;i<=NF;i++) sum[i]+=$i;};END {for (k=1;k<=NF;k++) print sum[k];}' t.txt ------->2

Also not able to understand why using this bracket
Code:
 [] is giving correct solution.

although i have achieved my target but not able to understand the detail working of the command.
as in both the command i am using NF but still in command 1 it sum up the row and not column.

scriptor
# 2  
Old 1 Week Ago
awk works in a way that it reads one input record (one line by default), executes the entire script on that line, and the proceeds to the next record.

In command 1, for every single input line you are summing up all fields in that line into a scalar variable, and then print (and reset) that variable.
In command 2, in lieu of using a scalar variable, you are using an array (indicated by the square brackets) indexed by the field No. to sum up the respective fields' values for ALL input lines, and print the sums in the END section.
See the difference now? Good reading is man awk.

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Using awk within a for loop Allie_gastrator Shell Programming and Scripting 2 08-21-2016 02:50 AM
awk for loop help Rabu Shell Programming and Scripting 13 01-26-2015 11:01 AM
awk programming -Passing variable to awk for loop ctrld Shell Programming and Scripting 2 08-15-2014 10:12 AM
awk for loop Makarand Dodmis Shell Programming and Scripting 4 05-19-2014 03:32 PM
For loop using awk helpless Shell Programming and Scripting 3 04-25-2014 03:55 PM
awk loop using array:wish to store array values from loop for use outside loop klane Shell Programming and Scripting 2 12-19-2013 04:25 PM
Using awk in a for loop evelibertine UNIX for Dummies Questions & Answers 3 05-02-2013 03:26 AM
For loop + awk Junaid Subhani Shell Programming and Scripting 2 04-12-2013 01:31 PM
awk loop and using shell in awk xshang Shell Programming and Scripting 4 10-18-2012 02:40 PM
using awk loop ken002 Shell Programming and Scripting 5 04-28-2011 09:02 PM
awk - loop from a to z jolecanard Shell Programming and Scripting 6 04-18-2011 10:02 PM
Comparison and editing of files using awk.(And also a possible bug in awk for loop?) linuxkid Shell Programming and Scripting 1 07-22-2010 05:37 AM
awk for-loop and NR ergy1983 Shell Programming and Scripting 8 06-25-2010 08:52 PM
Using AWK in a for loop Jahn Shell Programming and Scripting 4 09-17-2009 05:57 AM
While-loop with awk baghera Shell Programming and Scripting 13 08-29-2007 07:26 AM