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?

4
  • 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. Commented Nov 11, 2015 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. Commented Nov 11, 2015 at 14:45
  • @Hastur Unfortunately there is no header. I tried to used 'bc' but I didn´t succed to get the right output Commented Nov 11, 2015 at 14:48
  • There is even the internal awk solution (gawk)... Updated answer. Commented Nov 11, 2015 at 15:33

2 Answers 2

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
4
  • 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? Commented Nov 11, 2015 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. Commented Nov 11, 2015 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! Commented Nov 11, 2015 at 16:04
  • My fault... I forgot the shell execution $( ). Updated :) Commented Nov 11, 2015 at 16:14
1

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

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.