Calc max of a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calc max of a column
# 1  
Old 03-30-2010
Calc max of a column

In C that was easy with a for and if. Iam trying to learn a litle more in bash.

Example

Ronaldo:5800
Figo:4000
Rafael:2321
Kaka:1230

I want the max of the $2 and the output will be:

The max value is 5800 from Ronaldo.

How can i do this in shell?
Thanks for all, folks.

Last edited by rafazz; 03-30-2010 at 02:46 PM..
# 2  
Old 03-30-2010
Code:
$
$ cat f2
Ronaldo:5800
Figo:4000
Rafael:2321
Kaka:1230
$
$ cat f2.sh
#!/usr/bin/bash
IFS=:
while read name amt
do
  [[ "$amt" -ge "${MAX}" ]] && MAX=$amt
done <f2
echo "Max value = $MAX"
$
$ ./f2.sh
Max value = 5800
$
$

tyler_durden
# 3  
Old 03-30-2010
I am getting the content from file.sh
Code:
name:price
Ronaldo:5800
Figo:4000
Rafael:2321
Kaka:1230

I write this..
Code:
while read name price
do
  [[ "$price" -ge "${MAX}" ]] && MAX=$price
done <$price
echo "Max value = $MAX" ;

The error..
Code:
./file.sh: line 29: $price: ambiguous redirect


Last edited by Scott; 03-30-2010 at 06:14 PM.. Reason: Please use code tags
# 4  
Old 03-30-2010
Quote:
Originally Posted by rafazz
...
The error..

./file.sh: line 29: $price: ambiguous redirect
You will have to redirect from your data file, not from a value of a variable.

tyler_durden
# 5  
Old 03-30-2010
Code:
$ sort -t":" -knr2 file | IFS=":" read x y
$ echo $y
5800

Code:
awk -F":" ' $2>s{s=$2} END{print s} ' file

# 6  
Old 03-30-2010
Quote:
Originally Posted by rafazz
Iam getting the content from file.sh
nameSmilierice
Ronaldo:5800
Figo:4000
Rafael:2321
Kaka:1230

I write this..

while read name price
do
[[ "$price" -ge "${MAX}" ]] && MAX=$price
done <$price
echo "Max value = $MAX" ;

The error..

./file.sh: line 29: $price: ambiguous redirect
Look at durden_tyler's code more closely. It sets IFS to the value necessary to properly split the lines with read. It looks like you omitted the IFS modification. If so, without any whitespace in the line, the entire line (sans leading whitespace) will be assigned to $name. $price will be empty.

Also, as he mentioned above, you're trying to redirect from the wrong place. $price does not contain the name of the file to read.

Regards,
Alister

Last edited by alister; 03-30-2010 at 05:56 PM..
# 7  
Old 03-30-2010
Code:
awk -F":" ' $2>s{s=$2} END{print s} ' file

[/QUOTE]

In this case, the result is the print of my first line of the text, that is name : price. how can i make the prog to start read in second line?

file.sh
name : price
Ronaldo:5800
Figo:4000
Rafael:2321
Kaka:1230
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies

2. Shell Programming and Scripting

Get the MAX value out of a column

I've the following data set. I would like to look at the column 3 and only use the rows which has the max value for column 3 Can we use the awk or sed to achieve it. 10 2 10 100 11 2 20 100 12 2 30 100 13 2 30 100 14 ... (7 Replies)
Discussion started by: rudoraj
7 Replies

3. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

4. UNIX for Dummies Questions & Answers

get max value every 4 rows between 2 column

Hi all I have a file that has two columns and I need the maximum value in column 2 of 4 positions o rows. for example at position {1..3} there are 4 characters (A, C, G and T) each of these characters with a value with a value in column 2. I need the maximum value in column 2 and the corresponding... (2 Replies)
Discussion started by: xinox
2 Replies

5. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 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

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

9. Shell Programming and Scripting

Search max value in a column in a file instead of sort

Hi Everyone, 1.txt 00:00:00 0 0 0 0 0 0 0 00:00:01 0 0 0 2 1 33 2 00:00:02 5 0 0 0 0 0 0 00:00:03 0 4 0 0 0 0 0... (5 Replies)
Discussion started by: jimmy_y
5 Replies

10. 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