Search code examples
awktail

How to print the last lines from several files?


I tried:

awk 'FRN==NR {print FILENAME, $0} {nextfile}' *.asc > end
awk 'END {print FILENAME, $0} {nextfile}' *.asc > end1
df --total | tail -1 | awk '{print FILENAME, $0}' *asc > end2

The first command creates an empty file, the second is too long - there are not only the last lines and the third print only one line. Thank you

Example:

file1.asc

1
2
3

file2.asc

7
45
6

desired result:

3
6

Solution

  • Utilize the ENDFILE rule if you have GAWK.

    awk 'ENDFILE{print FILENAME, $0}' *.asc