Modifying awk code to be inside condition

I have the following awk script and I want to change it to be inside a condition for the file extension.

################################################################################

#  abs: Returns the absolute value of a number

function abs(val) {

return val > 0 ? val  \
               : -val

}

################################################################################

BEGIN {

  ARGV[ARGC++] = ARGV[ARGC-1]                                  # Read file twice

}

# --Read file: First time round-------------------------------------------------

FNR == NR {
  />/ && idx[FNR] = ++i
  $2 || val = $1                                 # Set source location ($2=0)
  ++j                                                   # Store last line number
  next
}

# --Read file: Second time round------------------------------------------------

FNR in idx { v = val[idx[FNR]] }                           # Get source location

{ srdist = abs($1 - v) }                       # Set source to receiver distance

/^[0-9]+/ && srdist > dsrmx {                                      # Print error
  print "  FNR="FNR", dist("v","$1")="srdist
}

################################################################################

Thus I have rewritten the code as

################################################################################

#  abs: Calculates the absolute value of val

function abs(val) {

  return val > 0 ? val  \
                 : -val

}

################################################################################

# Change record Separator and Field Separator.

BEGIN {

  split(ARGV[1], a, ".")
  fext = a[2]

  if (fext == "xt") {

    ARGV[ARGC++] = ARGV[ARGC-1]                                # Read file twice

  }

}

#-------------------------------------------------------------------------------

fext == "xt" {

  # Read file first time

  if (FNR == NR) {
      if ($0 ~ />/) { idx[FNR] = ++i }
      $2 || val = $1                             # Set source location ($2=0)
      ++j                                               # Store last line number
      next
  }

  # Read file second time

  if (FNR in idx) { v = val[idx[FNR]] }                    # Get source location

  { srdist = abs($1 - v) }                     # Set source to receiver distance

  if ($0 ~ /^[0-9]+/ && srdist > dsrmx ) {
      print "  FNR="FNR", dist("v","$1")="srdist
  }

}

################################################################################

I just have problem with the line

  { srdist = abs($1 - v) }                     # Set source to receiver distance

How can I include it in the code?

I am thinking of just removing the {}, thus having

################################################################################

#  abs: Calculates the absolute value of val

function abs(val) {

  return val > 0 ? val  \
                 : -val

}

################################################################################

# Change record Separator and Field Separator.

BEGIN {

  split(ARGV[1], a, ".")
  fext = a[2]

  if (fext == "xt") {

    ARGV[ARGC++] = ARGV[ARGC-1]                                # Read file twice

  }

}

#-------------------------------------------------------------------------------

fext == "xt" {

  # Read file first time

  if (FNR == NR) {
      if ($0 ~ />/) { idx[FNR] = ++i }
      $2 || val = $1                             # Set source location ($2=0)
      ++j                                               # Store last line number
      next
  }

  # Read file second time

  if (FNR in idx) { v = val[idx[FNR]] }                    # Get source location

  srdist = abs($1 - v)                         # Set source to receiver distance

  if ($0 ~ /^[0-9]+/ && srdist > dsrmx ) {
      print "  FNR="FNR", dist("v","$1")="srdist
  }

}

################################################################################

Your approach isn't working because fext is being set before any input lines are seen. Thus, fext is true if and only if the first argument matches a file with the extension "st". It wont matter what the rest of the arguments are. Further, the FNR==NR mechanism works only to distinguish the very first file from the rest. If you add files to the argument list, they will all be processed as if they were the first file read the second time.

My plan is that I pass only one file. but I need to check that the extension is .xt

The smarter thing is to wrap this in a shell script:

infile=$1
extension=${infile##*.}

if [ $# != 1 -o ."$extension" != ".xt" ]; then
   echo "Usage: $0 input.xt"
   exit 1
fi
exec awk -f yourawkscript "$infile"

If you do not have access to bash or ksh for some reason, then you can use this line to get the extension:

extension="`echo $infile| sed 's/.*\.//'`"

But if you need awk to do it then in your awk script, modify the BEGIN { ... } block to this:

BEGIN {

  if ( !match(ARGV[ARGC],/\.xt$/) ) { 
           print "Usage: awk -f thisfile input.xt"; exit(1); 
  }

  ARGV[ARGC+1] = ARGV[ARGC]                                  # Read file twice
  ++ARGC
}

Thanks a lot :b: