O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

FILENAME

FILENAME is set each time a new file is read. It stores the name of the current input file being read. This can be used when multiple files need to be parsed by AWK. When no data files are given on the command line, AWK reads from stdin and FILENAME is set to "-". In that case, you have to enter records in standard input, and once you are done, press Ctrl + C to stop reading from stdin. Let us begin with a very basic example:

$ awk 'END{print FILENAME}' cars.dat

The output of the execution of this command is as follows:

cars.dat

Now we will process the two files, cars.dat and emp.dat, and print some data from these files, as follows:

$ vi filename.awk#!/usr/bin/awk -fBEGIN   { f1="";              f2="";        }{if ( FILENAME == "cars.dat" ){print NR, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required