[awk] Get line numbers for each input file

Created on November 30, 2026

Use FNR for the current line number within the current file. Suppose you have these two files.

fruits.txt

apple
orange

vegetables.txt

carrot
tomato

Print FNR and the full line for each record.

awk '{print FNR, $0}' fruits.txt vegetables.txt
1 apple
2 orange
1 carrot
2 tomato

FNR returns to one at the start of each file. NR continues counting across all input files.

awk Shell command