How to compare a field value of a same column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare a field value of a same column?
# 1  
Old 05-25-2008
How to compare a field value of a same column?

I have a file which has entries like as below,

10:29:07.32 GOOD 122
10:29:07.35 GOOD 124
10:29:09.97 BAD 122
10:29:09.97 BAD 124
10:29:18.66 GOOD 128
10:29:19.12 BAD 128
10:30:23.20 GOOD 148
10:30:23.23 GOOD 156
10:30:25.55 BAD 148
10:30:25.55 BAD 156
10:30:35.53 GOOD 167
....
.......
..........


I need the following info from this, can anyone suggest a shellscript for this?

1) No of GOOD :

2) NO of BAD:

3) Time Diff(refer 1st column) of (GOOD-BAD) which has the same field value in the third column? (seperate one)

4) Total Time Diff between GOOD-BAD

Thanks!

How to compare a field value of a same column?
# 2  
Old 05-25-2008
One part of your query:

Code:
$ awk '{count[$2]++}END{for(j in count) print j,"("count[j]")"}' gobinath.txt
BAD (5)
GOOD (6)

//Jadu
# 3  
Old 05-25-2008
or

Code:
$ awk '$2 ~ /BAD/{++c} END {print c}' gobinath.txt

Code:
$ awk '$2 ~ /GOOD/{++c} END {print c}' gobinath.txt

and regarding your query:
How to compare a field value of a same column?

could you please put an example what you want to compare, would be very helpful to answer.

//Jadu
# 4  
Old 05-25-2008
Thanks for your immediate response....

I need the output like this,

Time diff of 122 : (10:29:09.97-10:29:07.32) = 00:00:02.65
Time diff of 124 : (10:29:09.97-10:29:07.35) = 00:00:02.65
Time diff of 128: (10:29:19.12-10:29:18.66) = 00:00:00:46
Time diff of 148:
Time diff of 156:
Time diff of 167:
================================================
Consolidated time diff= 00:00:05:76

Thanks!
# 5  
Old 05-26-2008
file name is a

Code:
awk '{
if($2=="GOOD")
	good++
else
	bad++

if (a[$3]=="")
	a[$3]=$1
else
	a[$3]=sprintf("%s-%s",$1,a[$3])
}
END{
print "No of GOOD:"good
print "No of BAD:" bad
for (i in a)
{
	print "Time diff of "i" "a[i]
}
}' a > a.t

sed '1,2d' a.t | cut -d' ' -f 5 |sed 's/\./:/g;s/-/:/' | awk 'BEGIN{FS=":"}
{
	d=$4-$8
	if (d<0)
	{
		d=100+d
		c=$3-$7-1
	}
	else
		c=$3-$7
	if (c<0)
	{
		c=60+c
		b=$2-$6-1
	}
	else
		b=$2-$6
	if (b<0)
	{
		b=60+b
		a=$1-$5-1
	}
	else
		a=$1-$5
	print a":"b":"c":"d
}'> a.tt

nawk '{
if(NR==FNR)
	a[NR]=$0
else
{
	if (NF<5)
		print
	else
	{
		$5=a[FNR-2]
		print
	}
}
}' a.tt a.t
rm a.t a.tt

# 6  
Old 05-29-2008
Thanks to all for your solution...it is working
# 7  
Old 05-06-2009
I have an almost similar problem, but in this case the fields 122, etc can re-occur more than once.

10:29:07.32 GOOD 122
10:29:07.35 GOOD 124
10:29:09.97 GOOD 122
10:29:09.97 BAD 124
10:29:18.66 GOOD 128
10:29:19.12 BAD 128
10:30:23.20 GOOD 148
10:30:23.23 GOOD 156
10:30:25.55 GOOD 148
10:30:25.55 BAD 156
10:30:35.53 GOOD 167
10:30:25.55 BAD 122
10:30:35.53 BAD 122
10:30:25.55 BAD 148
10:30:25.55 BAD 148


How can I consolidate the total time for a record (ie 122), less any total time that is flagged as Bad?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

4. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

5. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

6. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

7. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

10. Shell Programming and Scripting

Compare files column to column based on keys

Here is my situation. I need to compare two tab separated files (diff is not useful since there could be known difference between files). I have found similar posts , but not fully matching.I was thinking of writing a shell script using cut and grep and while loop but after going thru posts it... (2 Replies)
Discussion started by: blackjack101
2 Replies
Login or Register to Ask a Question