How to excute an awk file

Hi All,
Greetings.
I am working with an awk file say "Test.awk".
My Input file is say "test"

I am executing the awk file using

awk -f Test.awk test

I am new to awk. I wanted to know how can we execute the above awk command from a ksh file. Please help me on this.

Thanks in advance!!

Either like you wrote it already or use something like

#!/usr/bin/ksh

...
<maybe some more shell code>
...
awk -f Test.awk test
...
<maybe some more shell code>
...

exit 0

Why don't you just test it or did I misunderstand?

If you have done interactive commandline on the terminal and it works, you can write it also to the shell script. And vice versa. Interactive shell = read shell script from terminal = stdin. Shell script = read commandlines from file, not from stdin.

Thanks all..
I am having one more doubt..
Can I execute the awk file in a loop from ksh file.
For example

#!/bin/ksh
while read file
do
echo $file
echo "Total Number of Records in " $file ":"`wc -l $file |awk '{print $1}'`
awk -f Test.awk $file
done < filelist

where as filelist contains the list of input files..
I have tried this by giving list of 3 files in the filelist..But,It is not working. Please help
It is executing only the first file in the list

show the content of filelist... are you putting them in a single line??

Hi Rakesh..
I have rectified that. It was due to the space issue in the filelist file.

One more thing,
Can we able to get the name of the input file name in the awk file?
What I mean ,

awk -f test.awk inputfile

we are executing the above command from ksh..By any chance can we get the value of the input file inside the awk file?
Hope my question is clear :slight_smile:

#!/bin/ksh
while read file
do
   # echo result ok ?
   echo "$file"
   lines=$( wc -l < "$file" )
   echo "Total Number of Records in $file: $lines"
   # set variable filename and use it in your Test.awk or use builtin variable FILENAME
   awk -v filename="$file" -f Test.awk "$file"
done < filelist

GNU Awk-doc

Output without awk line. If you have still problem, give cut&paste filelist and output.

Hi Kshji,
I want the value of inputfilename i.e., in the above case $file to be used in Test.awk. How can I access that value inside Test.awk

Just I said:"
use filename in your Test.awk or use builtin variable FILENAME"

ex.

x=filename
x=FILENAME

Did you try ?

Hi ,
Actually I could not understand the previous post clearly. Now I understood and tried it. It is working. Thanks a lot!!!