Count first column on the basis of two other columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count first column on the basis of two other columns
# 1  
Old 05-29-2008
Count first column on the basis of two other columns

Hello,

I have a file
=================
12 SRV1 GRP1
19 SRV1 GRP1
19 SRV1 GRP2
3 SRV1 GRP1
3 SRV1 GRP2
30 SRV1 GRP2
7 SRV1 GRP1
8 SRV1 GRP3
===========

I want output like
===============
41 SRV1 GRP1
52 SRV1 GRP2
8 SRV1 GRP3

Basically I want to calculate the count of first column based on other 2 columns.

Thanks in advance.
# 2  
Old 05-29-2008
Something like that ?
Code:
awk '
{ sum[$2 OFS $3] += $1 }
END { for (f in sum) print sum[f],f }
' inputfile > outputfile

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies

2. Shell Programming and Scripting

Group/concatenate certain column and basis on this do addition on other column

Hi Experts, Need your support I want to group/concatenate column 1,2,12 and 13 and if found duplicate then need to sum value of column 17,20,21 and column22. After concatenation if found unique then no action to be taken. Secondly want to make duplicate rows basis on grouping/concatenation of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Script to count files on daily basis

Hi all I need to count files on a daily basis and send the output via email, what I am currently doing its simply running the following:ls -lrt *.cdr | awk '{if(($6 == "Jul") && ($7 == "13")) print $0}' | wc -l, which in this case it will count all files with extension "cdr" from the month of... (13 Replies)
Discussion started by: fretagi
13 Replies

4. Shell Programming and Scripting

Merge Two files on the basis of 2 columns at diffrent position

Hello, I am trying to merge two files with multiple records having a common columns but on first file its on 7th column and on 2nd file it on 6th column. First file(file1.txt) - 7th Column is unique H|123|Alex|Ren|W|9856746|2345789|20152202| H|97654|Ray|John||9855678|2345790|20152201|... (6 Replies)
Discussion started by: Mannu2525
6 Replies

5. Shell Programming and Scripting

Insert Columns before the last Column based on the Count of Delimiters

Hi, I have a requirement where in I need to insert delimiters before the last column of the total delimiters is less than a specified number. Say if the delimiters is less than 139, I need to insert 2 columns ( with blanks) before the last field awk -F 'Ç' '{ if (NF-1 < 139)} END { "Insert 2... (5 Replies)
Discussion started by: arunkesi
5 Replies

6. UNIX for Dummies Questions & Answers

Split a column into multiple columns at certain character count

Hey everyone, I have an issue with a client that is passing me a list of values in one column, and occasionally the combination of all the values results in more than an 255 character string. My DB has a 255 character limit, so I am looking to take the column (comma delimited file), and if it... (1 Reply)
Discussion started by: perekl
1 Replies

7. Shell Programming and Scripting

Count Columns and If less add new column

Hi All, My Input.txt should have always 4 columns in some cases i may not get all the 4columns data. My requirement is if columns as not equal to 4 then append new column delimiter. Input.txt A,1,2 B,1,2,3 C,1 Desired output should be in below format Output.txt A,1,2,... (3 Replies)
Discussion started by: kmsekhar
3 Replies

8. Shell Programming and Scripting

print least value of a column on the basis of another column

Hi, I am new to linux... I have a file which looks like: I want to print the entire row in which 5th column is having minimum value for every first column (i.e min for 9 and min for 16). Along with the condition awk -F" " 'b < $5 {b=$5; a=$0} END {for (i in a) {print a}}' inputfile >... (5 Replies)
Discussion started by: CAch
5 Replies

9. Shell Programming and Scripting

print least value of a column on the basis of another column

Hi, I am new to linux... I have a file which looks like: I want to print the entire row in which 5th column is having minimum value for every first column (i.e min for 9 and min for 16). Along with the condition awk -F" " 'b < $5 {b=$5; a=$0} END {for (i in a) {print a}}' inputfile >... (1 Reply)
Discussion started by: CAch
1 Replies

10. Shell Programming and Scripting

Delete Duplicates on the basis of two column values.

Hi All, i need ti delete two duplicate processss which are running on the same device type (column 1) and port ID (column 2). here is the sample data p1sc1m1 15517 11325 0 01:00:24 ? 0:00 scagntclsx25octtcp 2967 in3v mvmp01 0 8000 N S 969 750@751@752@ p1sc1m1 15519 11325 0 01:00:24 ? ... (5 Replies)
Discussion started by: neeraj617
5 Replies
Login or Register to Ask a Question