Summing by common header


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing by common header
# 1  
Old 12-19-2012
Summing by common header

hi guys,

Any quick solution for this?
i want to sum my row values in a matrix by common column header.
The values start from column 3.

sample input

Code:
        A-V_19    B-V_1    C-V_1    A-V_2    B-V_2    A-V_3    C-V_3
text1    text2    1    2    3    5    5    3    5

desired output

Code:
        A-V    B-V    C-V 
text1    text2    9    7    8


Last edited by newbie83; 12-19-2012 at 03:25 PM..
# 2  
Old 12-19-2012
Try this:
Code:
awk 'NR==1{for (i=1;i<=NF;i++) {sub("_.*","",$i);a[i]=$i}}
NR>1{x=$1" "$2;for (i=3;i<=NF;i++) s[a[i-2]]+=$i}
END{for (i in s) printf i" ";printf "\n"x" ";for (i in s) printf s[i]" ";printf "\n"}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-19-2012
Sorry I was not clear about this, this has to be done for each row,

so if there is another row in input

Code:
        A-V_19    B-V_1    C-V_1    A-V_2    B-V_2    A-V_3    C-V_3
text1    text2    1    2    3    5    5    3    5
text3    text4    1    2    3    5    5    7    9

desired output will be
Code:
        A-V    B-V    C-V 
 text1    text2    9    7    8
text3    text4    13   7    12

# 4  
Old 12-19-2012
Try:
Code:
awk 'NR==1{for (i=1;i<=NF;i++) {sub("_.*","",$i);a[i]=$i;h[$i]=1};for (i in h) printf i" ";printf "\n"}
NR>1{x=$1" "$2;for (i=3;i<=NF;i++) s[a[i-2]]+=$i;printf x" ";for (i in h) printf s[i]" ";printf "\n";for (i in h) s[i]=0}' file

This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Merge multiple files with common header

Hi all, Say i have multiple files x1 x2 x3 x4, all with common header (date, time, year, age),, How can I merge them to one singe file "X" in shell scripting Thanks for your suggestions. (2 Replies)
Discussion started by: msarguru
2 Replies

3. Shell Programming and Scripting

Manipulate all rows except header, but header should be output as well

Hello There... I have a sample input file .. number:department:amount 125:Market:125.23 126:Hardware store:434.95 127:Video store:7.45 128:Book store:14.32 129:Gasolline:16.10 I will be doing some manipulations on all the records except the header, but the header should always be... (2 Replies)
Discussion started by: juzz4fun
2 Replies

4. Shell Programming and Scripting

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

summing from two different files

I have two files hhhh 3674.00 a 75 1535 183 2134 291 2452 442 2738 704 3048 a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610 a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985 ... (4 Replies)
Discussion started by: Indra2011
4 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

7. Shell Programming and Scripting

Renaming all header to specific header pattern

Input #HAC0253 EFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #BASFS12 EFVEFVHIJEFVEFVTOPKEFVOPKTHIJTTHIJOPK #ACG5115 TEFVEFVOIJEFVHIJHIJOPKOPKHIJHIJTTEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK #ECG5114 IJTOPKHIJEFVOEFVEFVOPKTTEFVEFVOPKHIJOPKOPKOPK . . Output (5 Replies)
Discussion started by: patrick87
5 Replies

8. Shell Programming and Scripting

Summing amounts

Hi I need to sum of sub total amounts. Please any ideas file1.txt type: xx xyz 200 300 xyz1 300 400 Sub total 500 700 type:yy abc 200 300 abc1 100 100 Sub total ... (4 Replies)
Discussion started by: mohan705
4 Replies

9. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies
Login or Register to Ask a Question