fatal: cannot open file `TNAME' for reading (No such file or directory)

Hi,
I am running this command through a shell script and getting the error mentioned in the subject line:

testing.awk -f x.txt TNAME

My testing.awk file contains something like
++++++++++++++++++

#!/usr/bin/awk -f

BEGIN{
        TAB_NAME="INSERT_ONE_" ARGV[ARGC-1] ;
}

if ( $1=="JAM_ONE" && $2=="Starting" ) {
                print "insert into " TAB_NAME " (table_name) values('" $6 "');"

                print "\n";
        }
if ( $1=="JAM_TWO" && $2=="Finishing" ) {
                print "insert into " TAB_NAME " (table_name) values('" $6 "');"

                print "\n";
        }


....some other piece of code..

++++++++++++++++++

When i am trying to run this code:

testing.awk -f x.txt TNAME

It gives me a error (which i think is because of the fact it is trying to see TNAME as a file).

awk: testing.awk:32: (FILENAME=./X/X//x.txt FNR=4) fatal: cannot open file `TNAME' for reading (No such file or directory)

Can anyone suggest how to resolve the error? How to modify my code such that the last parameter in the command given above doesn't take it as a file name. I think it is trying to resolve the last parameter as a file and throwing this error.
I call the above command through a different shell script's for loop

for file_name in $xdir/*.done
        do
                if [[ -f $file_name ]]
                then
                testing.awk -f x.txt TNAME >> $command_file
                fi
       done
testing.awk -f x.txt TNAME

tells awk to process the file TNAME, which it cannot find

-f switch is used to tell awk to read the commands from file. So you probably wanted to call it like this:

awk -f testing.awk x.txt

You're still gonna get into trouble though... I'd suggest you use -v switch to awk to pass it a variable that you can use inside your script:

awk -v "TNAME=blah" '{print "TNAME has value: " TNAME}'

Perhaps you wanted to do this:

testing.awk -v "TNAME=blah" x.txt

to process the file x.txt with variable TNAME set to value "blah"

1 Like