Awk numeric range match only one digit?

Hello,

I have a text file with lines that look like this:

1974 12 27 -0.72743 -1.0169 2 1.25029 
1974 12 28 -0.4958 -0.72926 2 0.881839 
1974 12 29 -0.26331 -0.53426 2 0.595623 
1974 12 30 7.71432E-02 -0.71887 3 0.723001 
1974 12 31 0.187789 -1.07114 3 1.08748 
1975 1 1 0.349933 -1.02217 3 1.08041 
1975 1 2 0.401935 -1.21842 3 1.28301 
1975 1 3 0.545325 -1.0697 3 1.20068 
1975 1 4 0.240114 -1.05894 3 1.08583 

I want awk to search the 2nd field (which represents months) and spit out lines where that field's numeric value falls within a certain range. My code for this is:

echo "Please pick a numerical range (e.g. 6-9 for Jun-Sep)"
read months
awk '$2 ~ /['$months']/ {print $0}' file1.txt > file2.txt

This works great for, say, the range 6-9, but the problem is that for a month system, the numbers 1 and 2 will be matched in the numbers 10, 11, and 12, so a search for "1-3" (January-March) will return October, November, and December values that I don't want. I have tried forcing only a 1-digit match using:

awk '$2 ~ /['$months']{1}/ {print $0}' mjo-tmp0.txt > mjo-tmp1.txt

...but that doesn't work(it returns nothing), and even if it did, it would eliminate searches for double-digit months like "10-12". Is there a way for me to get awk to search only for exact matches within the numerical range? Any help would be appreciated.

Try comparing $2 as a number, not a regex.

awk 'BEGIN { MIN=1; MAX=9; }
($2 >= MIN) && ($2 <= MAX)'

The { print $0 } is redundant if all you want is print $0 . That's what happens by default given no code block.

---------- Post updated at 01:38 PM ---------- Previous update was at 01:36 PM ----------

Also, if you have -v, that's a safer and simpler way to get variables into awk:

awk -v MIN=1 -v MAX=9 '($2 >= MIN)&&($2 <= MAX)'

Thank you very much. This is the framework I needed.

1 Like