awk - Compare files in two different directories

Hi,
My script works fine when I have both input files in the same directory but when I put on of the input file in another directory, the output does not show up.

SCRIPT:

awk '
  BEGIN {
    OFS="\t"
    out = "File3.txt"}
NR==FNR && NF {a[substr($0,1,8)]=$0; next}
function print_77_99() {
    if (key in a) 
      print "77", line[key] > out
}
 $1 == "01" {
    if (FNR > 1) print_77_99()
    key = $4 $3 $2
 lines = ""  
}
  { print > out
    lines = lines $0 "\n"  }  END {print_77_99()}
' File2.txt File1.txt

When I am trying to change the input directory path of File1.txt only, but the output File3.txt becomes empty.
Script works fine as long as both input files (File1.txt, File2.txt) are in the same directory.

EXAMPLE OF SCRIPT MODIFIED:

awk '
  BEGIN {
    OFS="\t"
    out = "File3.txt"
path-input = "/DR/m/"}
NR==FNR && NF {a[substr($0,1,8)]=$0; next}
function print_77_99() {
    if (key in a) 
      print "77", line[key] > out
}
 $1 == "01" {
    if (FNR > 1) print_77_99()
    key = $4 $3 $2
 lines = ""  
}
  { print > out
    lines = lines $0 "\n"  }  END {print_77_99()}
' File2.txt ${path-input}/File1.txt

Is there a better way to do it?

Any help is appriciated.
Thanks,

File1.txt

01  89  68  5000
02  83  11
04  83  9   02
03  83  00
06  83  00
07  83  11  RT0429
01  44  73  8800
02  44  73
04  44  73   02
03  44  73
06  44  73
07  44  11  RT  0789

File2.txt

50006889RT0429 NARD /3010  /E     /C A87545457          /  //                ///11        ///

51002387 NARD /3000  /E     /S N054896334IV          /  //                ///11        ///

Either of the following should work:
Change:

awk '
  BEGIN {
    OFS="\t"
    out = "File3.txt"
path-input = "/DR/m/"}

to:

path_input="/DR/m/"
awk '
  BEGIN {
    OFS="\t"
    out = "File3.txt"
}

and change:

' File2.txt ${path-input}/File1.txt

to:

' File2.txt ${path_input}/File1.txt

or go back to your original script and just change the last line to:

' File2.txt /DR/m/File1.txt
1 Like

You can't change the shell's expansion of the input stream (= all files including paths being parameters to awk ) from within the awk script: expansion is done before awk is run, and awk does not "export" its internal variables.
Apply Don Cragun's suggestions to make it work.

1 Like

Thanks Don.
You're method worked.
Thanks for assisting.
I appreciate your help.