Search code examples
bashif-statementawkintegersubstr

AWK - using substr integer as part of if condition


I'm attempting to use awk one liner to print lines of a file in which the substring is less than a defined variable. Also the line must start with the letter E. The E condition is working, but not the result for the simple if 'less than' I'm looking for. What am I doing wrong here?? It is incorporated into a larger bash script. Thanks in advance.

#!/bin/bash
minimum_dpt=50

awk -v depth="$minimum_dpt" '{if (/^E/ && int(substr($0,65,6)<depth)) print "Shot: ",substr($0,21,5)," has depth below minimum. Value: ",substr($0,65,6)'}

Input:

E1985020687     1 1  2942984632.99S 88 354.60E 596044.16185585.10000.9 44 826 9
E1985020687     1 1  2943264732.95S 88 359.24E 595917.26185461.80000.5 44 82727
E1985020687     1 1  2944264741.97S 88 450.86E 594520.36185751.92445.3 44 82846
E1985020687     1 1  2945264741.97S 88 450.86E 594520.36185751.90045.3 44 82846

Output:

Shot:   2942  has depth below minimum. Value:  0000.9
Shot:   2943  has depth below minimum. Value:  0000.5
Shot:   2945  has depth below minimum. Value:  0045.3

Solution

  • You probably intended:

    int(substr($0,65,6))<depth
    

    or even just:

    (substr($0,65,6)+0)<depth
    

    instead of what you have:

    int(substr($0,65,6)<depth)
    

    There's probably a better way to do this but without seeing your input and output idk...