How to print filename with awk on Linux / Unix

h‮w//:sptt‬ww.lautturi.com
How to print filename with awk on Linux / Unix

To print the filename with awk on Linux or Unix, you can use the FILENAME built-in variable. FILENAME is a predefined variable that contains the name of the current input file.

Here is an example of how you can use FILENAME to print the filename with awk:

awk '{print FILENAME}' file.txt

This will print the name of the file, file.txt, to the terminal.

You can also use FILENAME in an awk script to perform actions based on the filename. For example, the following awk script will print the filename and the first field of each line in the file:

awk '{print FILENAME ": " $1}' file.txt

This will print output like the following:

file.txt: field1
file.txt: field1
file.txt: field1
Created Time:2017-10-28 21:39:06  Author:lautturi