Operating System - HP-UX
1847563 Members
3874 Online
110265 Solutions
New Discussion

Re: need help in simple scripts

 
SOLVED
Go to solution
YLTan
Frequent Advisor

need help in simple scripts

I am trying to write a ksh script to do data comparison for a flat file. But the data is in decimal e.g. 56.48, 45.68, 56.22, etc.

I tried doing the following but it outputs only the last max value = 55.87. Can some tell me how to do decimal comparison using ksh scripts?

sort -r -k 4,4 xfrdGLOBAL.asc > xfrdGLOBAL.sort

cat xfrdGLOBAL.sort | sed -e '1,$s/ //g' > xfrdGLOBAL.nospace
max_cpu=0

for REC in `cat xfrdGLOBAL.nospace`
do
col_4th=`echo $REC | awk -F'|' '{print $4}'`

if [ $col_4th -gt $max_cpu ]
then
max_cpu=$col_4th
fi
done
echo ""
echo $max_cpu

exit 0

My data file is as follows
09/22/03|00:00| 300| 55.12|
09/22/03|00:05| 299| 54.93|
09/22/03|00:10| 299| 54.91|
09/22/03|00:15| 300| 54.93|
09/22/03|00:20| 299| 54.88|
09/22/03|00:25| 299| 54.88|
09/22/03|00:30| 299| 55.00|
09/22/03|00:35| 299| 54.91|
09/22/03|00:40| 300| 55.95|
09/22/03|00:45| 299| 54.94|
09/22/03|00:50| 299| 55.87|
09/22/03|00:55| 299| 54.88|




tyl
3 REPLIES 3
Mark Grant
Honored Contributor

Re: need help in simple scripts

Could you explain what you mean by data comparison. As far as I can see, your script appears to try and find the highest value out of a list. It does that successsfully so what are you trying to compare?
Never preceed any demonstration with anything more predictive than "watch this"
curt larson_1
Honored Contributor
Solution

Re: need help in simple scripts

the only thing i can see that might be a problem is the use of [...] which is obsolete. it has been replaced by [[...]] and ((...)).

try using
if (( "$col_4th > "$max_cpu" ))
then
....

but you could do this all within awk

sort -r -k 4,4 xfrdGLOBAL.asc | tr -d " " |
awk -F"|" '{
if ( $4 > max ) max = $4;
} END { print max;}'
curt larson_1
Honored Contributor

Re: need help in simple scripts

your sort should probably be:
sort -r -t"|" -k 4n,4

use "|" as the delimiter between keys
and 4n,4 sort by arithmetic value

and by sorting you probably don't need to do the comparision at all

sort -r -t"|" -k 4n,4 xfrdGLOBAL.asc |
awk -F"|" '{print $4;exit}'

or don't use sort at all

cat xfrdGLOBAL.asc |
awk -F"|" '{
if ( $4 > max ) max = $4;
} END {print $4;}'