0

I have a representative dataset

 35.5259  327   
 35.526  326   
 35.526  325  
 35.5261  324   
 35.5262  323   
 35.5263  322   
 35.5264  321   
 35.5265  320   
 35.5266  319  
 35.5268  318


# Contour 4, label:
 35.5269  317   
 35.527  316  
 35.5272  315  
 35.5274  314   
 35.5276  313   
 35.5278  312   
 35.528  311  

# Contour 4, label:
 35.5282  310  
 35.5285  309  
 35.5287  308  
 35.529  307  
 35.5293  306  

I try to find the two max values within a range in col 2 with:

awk '320>$2,$2>315 && $1>max1{max1=$1;line=$2} 313>$2,$2>307 && $1>max2{max2=$1;line2=$2} END {printf " %s\t %s\t %s\t %s\n",max1,line,max2,line2}' FILENAME

I just get blank ouput (As I have lots of blank spaces in the txt file)How to ignore that ? with $1+0 == $1 ?

I would like to find the Max values in col1 between 320 and 315 and between 313-307 in col2. The output I need:

35.5266  319   35.5278  312

How do I get the desired output ? Thanks

2
  • Could you tell us what the range is that you're interested in, and include data that are representative of your actual data ("has lots of blank spaces")? – Kusalananda Apr 27 '20 at 7:32
  • @Kusalananda I have edited the ques. – newstudent Apr 27 '20 at 7:47
1

Your code works well if you change , to &&.

But I think you have a logical error, too. Shouldn't $1>max1 rather be$2>line1 (and the same for max2/line2) ?

awk '
  320>$2 && $2>315 && $2>line1 {max1=$1;line1=$2}
  313>$2 && $2>307 && $2>line2 {max2=$1;line2=$2}
  END {printf " %s\t %s\t %s\t %s\n",max1,line1,max2,line2}
' file
1

I see that pLumo's answer does indeed give your sample output, but your question is about finding maximum values within column 1 range; the example output is printing the maximum value in column 2. So I think you've either mis-stated your question or your sample output. Also when you say "between" 320 and 315 are those numbers themselves included? I'll assume they are in this proposed solution (so >= operator); I'm using next to ignore any lines that don't have just 2 fields; I initialise my chosen variables with a BEGIN by way of good practice.

awk 'BEGIN {max1=max2=line1=line2=0}
     NF!=2 {next}
     320>=$2 && $2>=315 && $1>max1 {max1=$1;line1=$2}
     313>=$2 && $2>=307 && $1>max2 {max2=$1;line2=$2}
     END {printf " %s\t %s\t %s\t %s\n",max1,line1,max2,line2}' file.name.here
1
  • 1
    you don't need to skip these lines as they will get skipped automatically due to $2>=315 being false if $2 is unset. – pLumo Apr 27 '20 at 13:56

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.