add multiple colum using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add multiple colum using awk
# 1  
Old 06-19-2008
Java add multiple colum using awk

hi i need help in my script
i have a file
a.txt
3,4,5,13
6,7,8,45
9,0,1,67

i want to add 2nd and 3rd colum like o/p will be by adding all values of colum2(4+7+0) and colum 3(5+8+11)

o/p:

colum 2: 11
colum 3: 14
# 2  
Old 06-19-2008
Code:
awk -F"," '{col2+=$2;col3+=$3}END{print "column 2: "col2;print "column 3: "col3}' file

# 3  
Old 06-19-2008
Bug

Thanks for quick reply....Smilie

can u pls suggest me command if i will be having more than 2 coloums like
2,3,4,5,6,7,8,9,0,33,44,55,66
3,4,5,6,7,8,3,2,1,55,66,78,88


and if i have to add coloums from 2 nd to 9.
can we use for loop here to make it variable if requirement is for more than 10 coloums

Last edited by anish19; 06-19-2008 at 08:32 AM.. Reason: suggestion
# 4  
Old 06-19-2008
Code:
awk '{ for (i=1; i<=NF; ++i) sum[i] += $i; if (i > max) max=i }
END { s=""; for (i=1; i<=max; ++i) { printf "%s%s", s, sum[i]; s=OFS; } printf "\n" }' a.txt

# 5  
Old 06-19-2008
Bug

Hi thanks for reply but when i am running this command it gives o/p
>cat a.txt
2,3,4,5,6,7,8,9,0,33,44,55,66
3,4,5,6,7,8,3,2,1,55,66,78,88
>awk '{ for (i=1; i<=5; ++i) sum[i] += $i; if (i > max) max=i } END { s=""; for (i=1; i<=max; i++) { printf "%s%s", s, sum[i]; s=OFS; } printf "\n" }' a.txt
0 0 0 0 0

it not adding the coloums
# 6  
Old 06-19-2008
You need to specify the field separator -F, -- sorry for missing that. Maybe you also want s=IFS rather than s=OFS (or maybe not).
# 7  
Old 06-20-2008
Thanks era...........

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vlookup using awk without exact match for two colum input

Source file 1 335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345 335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345 335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345 335 R1-snapfound 009C1... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. Shell Programming and Scripting

awk to extract multiple values from file and add two additional fields

In the attached file I am trying to use awk to extract multiple values and create the tab-delimited desired output. In the output R_Index is a the sequential # and Pre_Enrichment is defaulted to .. I can extract from the values to the side of the keywords, but most are above and I can not... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Colum number where value is >0

Hi All I want to findout coloum number where value is >0 from below string Can we do using awk or any command ? Thanks in advance ... (7 Replies)
Discussion started by: aaysa123
7 Replies

4. Shell Programming and Scripting

Script by row colum

Hi All, how can output file1 to file2 by colum, e.g. $cat file1 1 2 3 $cat file2 aaa bbb ccc ddd eee fff ggg hhh iii then i need new file1 (4 Replies)
Discussion started by: aav1307
4 Replies

5. Shell Programming and Scripting

Sorting based on a particular colum

Hi, I want a flat file(pipe delimited) to be sorted based on 2nd column only. Below is input file to be sorted. AVERS|K50034|A|Y|N|N|Y|Y|Y|||N|N AVERS|K50035|A|Y|N|N|Y|Y|Y|||N|N... (11 Replies)
Discussion started by: Nikhath
11 Replies

6. Linux

Doubt on vmstat swpd colum

Dear Gurus, When i ran vmstat, i am getting swpd value,but si, and so beneath the swap is o, My doubt what is the difference between these values,i,e swpd and si and so OS is OEL 5.5 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache... (2 Replies)
Discussion started by: dave234
2 Replies

7. UNIX for Dummies Questions & Answers

joining files with different rows and colum

Hi all, I have two files: file one (9 rows, 3 columns): A 1 x1 B 2 f1 C 3 f3 D 4 u5 E 5 l9 F 6 h6 G 7 g4 H 8 r4 I 9 s2 file two (4 rows, 1 column): A B (2 Replies)
Discussion started by: anjas
2 Replies

8. Shell Programming and Scripting

sed command to delete points in a certain colum

Dear Unix users, I have a (I think simple) question; I have a file (out.dat) like below, the file contains some line which include 'LOS' string. . LOS 46.5360 91.0220 200708.2515.4900. 5400 64 1100010 . . I would like to delete the points in 4th... (4 Replies)
Discussion started by: mgunduz
4 Replies

9. Shell Programming and Scripting

find lines have 2nd colum value greater than 40

i have a file a.txt 12345,20 34567,10 23123,50 123456,45 how to find lines which hav 2nd entry greater than 40 o/p 23123,50 123456,45 pls help to get o/p (5 Replies)
Discussion started by: devesh123
5 Replies

10. Shell Programming and Scripting

sed or awk to convert text files with recurring headings to rows and colum

I have many text file reports generated by a Information Assurance tool that I need to get into a .CSV format or Excel tab delimited format. I want to use sed or awk to grab all the information in the sample text file below and create column headings:Risk ID, Risk Level, Category, Description, How... (5 Replies)
Discussion started by: Bjoeboo
5 Replies
Login or Register to Ask a Question