tags:

views:

38

answers:

2

I have file as shown below .I have to find the maximum value for each timestamp. So I have to find the max(2434, 2681, 2946, 1626) , max(3217, 4764, 4501, 3372) and so on ... (since these numbers have a common timestamp)

Timestamp  value

1280449531 2434
1280449531 2681
1280449531 2946
1280449531 1626
1280449532 3217
1280449532 4764
1280449532 4501
1280449532 3372
1280449533 4129
1280449533 6937
1280449533 6423
1280449533 4818
1280449534 4850
1280449534 8980
1280449534 8078
1280449534 6788
1280449535 5587
1280449535 10879
1280449535 9920
1280449535 8146
1280449536 6324
1280449536 12860
1280449536 11612
1280449536 9867

I wrote this code but getting errors. Can somebody correct me ? Thanks in advance

#!/bin/bash
awk '{ if [ temp -ne $1 ] 
       then temp = $1; big[$1] = $2
       fi
       elif [temp -eq $1] then if [$2 gt $big[$1] ] big[$1] = $2 ; fi 
       fi

     }' plots.dat   
+3  A: 
$ awk '$2>values[$1]{values[$1]=$2}END{for(i in values)print values[i],i } ' file
2946 1280449531
4764 1280449532
6937 1280449533
8980 1280449534
10879 1280449535
12860 1280449536
ghostdog74
Thanks a lot @ghostdog !
Sharat Chandra
@Sharat: if this answers your question properly, please accept it by clicking on the checkmark under the number of votes.
David Zaslavsky
+1  A: 

You're mixing AWK and Bash syntax and with errors in each.

Here's a pure Bash solution:

#!/bin/bash
while read -r timestamp value
do
    if (( value > ${array[timestamp]} + 0 ))
    then
        array[timestamp]=$value
    fi
done < plots.dat
for i in ${!array[@]}
do
    echo "$i ${array[i]}"
done
Dennis Williamson