How to print filename with awk on Linux / Unix

You can print the filename using the FILENAME variable in awk. Here’s an example:

awk '{ print FILENAME ": " $0 }' file1 file2

This command will print the content of file1 and file2, each line prefixed by the filename.

You can also use the ARGV array to iterate over all the files passed as arguments to awk. Here’s an example:

awk '{ print ARGV[ARGIND] ": " $0 }' file1 file2

This command will also print the content of file1 and file2, each line prefixed by the filename.

Leave a Comment