How to get the count only if two column matches?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the count only if two column matches?
# 1  
Old 05-27-2008
How to get the count only if two column matches?

Hi,

I have a file with the contents as below,
Code:
10:23:10        GOOD      10.30.50.60
10:23:11        GOOD      10.30.50.62
10:23:12        Hello        10.30.50.60
10:23:12        BAD         10.30.50.60
10:23:13        GOOD       10.30.50.66
10:23:14        BAD         10.30.50.62
10:23:15        Hello        10.30.50.66
10:23:16        Hello        10.30.50.66
10:23:16        BAD         10.30.50.66

I need the output like this,
Code:
No of Hello for  10.30.50.60: 1
No of Hello for  10.30.50.66: 2

Also
Code:
No of Hello for 10:23 (time) : 3

Please anyone suggest a script for this.

Thanks in advance

Last edited by Yogesh Sawant; 05-27-2008 at 04:29 AM.. Reason: added code tags
# 2  
Old 05-27-2008
Code:
$ awk '$2=="Hello" {count["Number of "$2" for "$3]++}END{for(j in count) print j":",count[j]}' gobi1.txt
Number of Hello for 10.30.50.60: 1
Number of Hello for 10.30.50.66: 2

//Jadu
# 3  
Old 05-27-2008
And,

Code:
$ awk '$2=="Hello" {count["Number of "$2" for "substr($1,1,5)]++}END{for(j in count) print j"(time):",count[j]}' gobi1.txt
Number of Hello for 10:23(time): 3

//Jadu
# 4  
Old 05-27-2008
Or:
Code:
awk '{a[$4 " in " $5]++
      b[$4" for "$1":"$2]++;next}
END{
for ( i in a )
   print "No of "i":" a[i]
print "---------------------------"
for ( i in b )
   print "No of "i"(time):" b[i]

}' FS="( )|(:)"file
No of GOOD in 10.30.50.60:1
No of GOOD in 10.30.50.62:1
No of GOOD in 10.30.50.66:1
No of BAD in 10.30.50.60:1
No of Hello in 10.30.50.60:1
No of BAD in 10.30.50.62:1
No of BAD in 10.30.50.66:1
No of Hello in 10.30.50.66:2
---------------------------
No of Hello for 10:23(time):3
No of BAD for 10:23(time):3
No of GOOD for 10:23(time):3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. UNIX for Dummies Questions & Answers

Search for partial matches in particular column

I have a list a b c d I want to search this list to have partial matches in column 2 in data file col1 col2 col3 1 a/e aa 2 b/e aa 3 z/y aa 4 t/u bb 5 d/f aa 6 a/t aa and extract the relevant rows with header (4 Replies)
Discussion started by: jianp83
4 Replies

3. Shell Programming and Scripting

Delete row if column matches

Hi, I have a long file in the format below. I want to delete the consecutive lines that contain the same value in column 1. I have tried awk '!x++' FS="," filename This has not worked. 14,MM709_BHP_DM,BHP,BHP_MC709_DM 19,OFFLINE,CHE,CHEV_MC773_DM 20,M33,BP,BP_MIM775_NS_DM ... (2 Replies)
Discussion started by: ndnkyd
2 Replies

4. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

5. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

6. Shell Programming and Scripting

How to get difference of the same column between two files when other column matches?

File 1: 20130416,235800,10.78.25.104,BR2-loc,60.0,1624,50.0,0,50.0,0 20130416,235800,10.78.25.104,BR1-LOC,70.0,10,50.0,0,70.0,0 20130416,235800,10.78.25.104,Hub_None,60.0,15,60.0,0,50.0,0 File 2: 20130417,000200,10.78.25.104,BR2-loc,60.0,1626,50.0,0,50.0,0... (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

7. Shell Programming and Scripting

awk - how to get difference of the same column when other column matches

I have a file like this : # cat list cucm, location,76,2 cucm1,location1,76,4 cucm,location,80,8 cucm1,location1,90,8 cucm1,location1,87,11 cucm,location,67,9 and I want output like this : cucm,location,76,2 cucm1,location1,76,4 cucm,location,80, 6 ===> (8-2 =6) cucm1,location1,90,4... (5 Replies)
Discussion started by: Lakshmikumari
5 Replies

8. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

9. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

10. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies
Login or Register to Ask a Question