1

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?

  • Since you're using awk to 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. – steeldriver Nov 11 '15 at 13:54
  • I suppose you have some "table" with the header in the first line... check it. head -n 1 */*txt or something similar. – Hastur Nov 11 '15 at 14:45
  • @Hastur Unfortunately there is no header. I tried to used 'bc' but I didn´t succed to get the right output – steve Nov 11 '15 at 14:48
  • There is even the internal awk solution (gawk)... Updated answer. – Hastur Nov 11 '15 at 15:33
1

Ok that is a workaround that should work

#! /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
      RESULT=$(awk 'FNR==1 {print $4, FILENAME}' ${model}_${gcm}_${scenario}_${river}[1-9]/${model}_${gcm}_${scenario}_${river}[1-9].txt | sort -n -r| head -1) 
      highest="$(echo $RESULT | cut -d ' ' -f1 )"
      hifile="$(echo $RESULT | cut -d ' ' -f2 )"
      echo "highest was $highest in $hifile"
      cp "$hifile" "/home/steve/high_test/${model}_${gcm}_${scenario}_${river}.txt"
    done
  done
done

The idea is inside the awk command over a bunch of files,

 awk 'FNR==1 {print $4, FILENAME}' *txt | sort -n -r| head -1 | cut -d ' ' -f2

where:

Ps> it's possible to think a solution without sort and head but only with awk.

awk 'FNR==1 {i++; A[i]=$4; B[i]=FILENAME} 
     END{ c=A[1];d=B[1];  
          for (j=2;j<i;j++){
            if (A[j]>c){c=A[j];d=B[j];}  
          } 
          print c,d ;
        }' *txt
  • It seems to work in finding the highest value. But something is wrong with the cp command cp: invalid option -- '2'. Do you have an idea why? – steve Nov 11 '15 at 15:48
  • @steve To which version did you exactly refer? Write an echo before the cp line ( echo cp ......) and you can read what it is wrong. – Hastur Nov 11 '15 at 15:51
  • I refer to the first solution with sort and cut. What´s wrong is that there is no input file to copy, and I can´t figured out why! – steve Nov 11 '15 at 16:04
  • My fault... I forgot the shell execution $( ). Updated :) – Hastur Nov 11 '15 at 16:14
1

If you have bc available then you could replace line 13 with

if [ $(echo "$highest<$fourth" | bc) = 1 ];then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.