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
}
}
################################################################################