count the max by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count the max by awk
# 1  
Old 11-28-2008
count the max by awk

hey everybody ..
I have a problem when I use this command to count the Max number by awk it give to me 99 while there are numbers bigger than 99


awk 'NR==0 { temp = $0 ;next}
{ if ( $0 >= temp ) { temp=$0 } }
END{ print "Maximum Number:"temp""}' file

thank you for your help ..
# 2  
Old 11-28-2008
awk uses a string comparison if you use $0 at this manner (strange behaviour?), a solution is to "cast" the variable as a integer:

Code:
awk '$0>int(m){m=$0}END{print "Max number: "m}' file

or to use fieldnumber $1:

Code:
awk '$1>m{m=$1}END{print "Max number: "m}' file

# 3  
Old 11-28-2008
thank you for your help

but it still not working when try with your command the max num appear 45 even less than the last time which 99
# 4  
Old 11-28-2008
It gives me the correct output:

Code:
$ cat file
1
99
2
3
45
5
7
15
1
2
0
$
$
$ awk '$1>m{m=$1}END{print "Max number: "m}' file
Max number: 99
$
$

Regards
# 5  
Old 11-28-2008
try a number more than 100 Smilie
# 6  
Old 11-28-2008
No problem, works fine:
Code:
$ cat file
99
45
5
7
101
15
199
2
0
$ awk '$1>m{m=$1}END{print "Max number: "m}' file
Max number: 199
$

# 7  
Old 11-28-2008
I am sorry for annoying you but could you please try these numbers
0
10
34
34
54
122
58747
54
99
2
1
45
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to count respon time max min avg for nginx logs?

Hi All, need your help, i want count respon time max and average my nginx logs, based on hourly or minutes per api... my nginx.log sample : 10.1.1.1 - - "POST /v2/api/find/outlet/ HTTP/1.1" 200 2667 "-" "okhttp/3.12.0" "118.215.153.47" 0.178 0.178 . 10.1.1.1 - - "POST... (4 Replies)
Discussion started by: fajar_3t3
4 Replies

2. UNIX for Beginners Questions & Answers

How to count average and max respon time?

sorry i will revise first (1 Reply)
Discussion started by: fajar_3t3
1 Replies

3. Shell Programming and Scripting

[Solved] Using awk to calculate max value

I have a file of sites and each site has a variable number of flow values with a date for each value. I want to determine the max value of flow for each site and output the site number, max value, and date of max value.The format structure (simplified) is the following: Record Site Number ... (5 Replies)
Discussion started by: cparr
5 Replies

4. Shell Programming and Scripting

awk to find the max

Experts, Here is my question. I have got file like below # cat file XYZb,24,26,6 XYZc,24,26,6 XYZe,24,25,5 XYZf,23,29,5 XYZi,16,25,5 XYZj,24,26,7 XYZn,17,23,4 XYZz,23,29,5 Now, I want to print the line's Column1 whose Column 3 is the max of all. My code is awk -F',' 'BEGIN {max... (9 Replies)
Discussion started by: sathyaonnuix
9 Replies

5. Shell Programming and Scripting

awk - count character count of fields

Hello All, I got a requirement when I was working with a file. Say the file has unloads of data from a table in the form 1|121|asda|434|thesi|2012|05|24| 1|343|unit|09|best|2012|11|5| I was put into a scenario where I need the field count in all the lines in that file. It was simply... (6 Replies)
Discussion started by: PikK45
6 Replies

6. Shell Programming and Scripting

How to find the average,min,max ,total count?

Hi , Below is my sample data,I have this 8 column(A,B,C,D,E,F,G,H) in csv file. A , B ,C ,D ,E ,F,G ,H 4141,127337,24,15,20,69,72.0,-3 4141,128864,24,15,20,65,66.0,-1 4141,910053,24,15,4,4,5.0,-1 4141,910383,24,15,22,3,4.0,-1 4141,496969,24,15,14,6,-24.0,-18... (7 Replies)
Discussion started by: vinothsekark
7 Replies

7. Shell Programming and Scripting

Join and awk max column

Hi Friends, I have a file1 with 3400 records that are tab separated and I have a file2 with 6220 records. I want to merge both these files. I tried using join file1 and file2 after sorting. But, the records should be (3400*6220 = 21148000). Instead, I get only around 11133567. Is there anything... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

8. Shell Programming and Scripting

Count time min/max/average for ping

I am redirecting my ping output to a file. The sample output is like this: 64 bytes from xx.xx.xx.167: icmp_seq=4490 ttl=116 3.75 ms 2011Jul12- 15 40 16 64 bytes from xx.xx.xx.167: icmp_seq=4491 ttl=116 5.29 ms 2011Jul12- 15 40 17 64 bytes from xx.xx.xx.167: icmp_seq=4492 ttl=116 4.88 ms... (6 Replies)
Discussion started by: zorrox
6 Replies

9. Shell Programming and Scripting

Max column count in a file

I have to send a file to mainframe and before sending it, I have to execute the quote command to set the record length. Since the file is dynamic, I do not know what the maximum size of a line could be. Currently, I use the following function to get the Max Column Count. Since I use "sed" it... (2 Replies)
Discussion started by: gemini
2 Replies
Login or Register to Ask a Question