I have a small bash program, that allows me to compare tables (having similar naming style) and that copies to another directory the file having the highest value at the first row of the fourth column (with space separator) (the values of the fourth column are decimal numbers e.g. 1111.22).
#! /bin/bash
river=lobith_rhine
highest=1
for model in H08
do
for gcm in IPSL-CM5A-LR
do
for scenario in hist rcp8p5
do
for x in ${model}_${gcm}_${scenario}_${river}[1-9]/${model}_${gcm}_${scenario}_${river}[1-9].txt
do
fourth="$(awk 'NR==1{print $4}' $x)"
if [ "$highest" -lt "$fourth" ];then
highest=$fourth
hifile=$x
fi
done
echo "highest was $highest in $hifile"
cp $hifile /home/steve/high_test/${model}_${gcm}_${scenario}_${river}.txt
done
done
done
Unfortunately, the line 13 if [ "$highest" -lt "$fourth" ];then produces the following error message:
integer expression expected
So, I read some documentation about comparisons operators, and found that I can replace the problematic line by
if [ "$highest" \< "$fourth" ];then
But this is not working properly since it just compares the ascii alphabetic order values rather than the entire value.
Does anyone has an idea about how to handle those issues?
awkto extract the value, why not use it to do the numeric comparison as well? With some re-work, you can probably replace the loop over files as well.head -n 1 */*txtor something similar.awksolution (gawk)... Updated answer.