print specific line from multiple files

 awk 'FNR==27 {print FILENAME, $0}' *.txt >output.txt
  • FILENAME is built-in awk variable for current input file name
  • FNR refer to line number of current file
  • $0 means whole line

Using a for loop:

{ for i in *.txt; do echo "$i : $(sed -n '27p' "$i")"; done ;} >output.txt

The for loop may be expensive as you have 5000+ files but given the current hardwares should not be a problem.


Faster way, quitting sed after line 27 (thanks @Fiximan):

{ for i in *.txt; do echo "$i : $(sed -n '27p;q' "$i")"; done ;} >output.txt

Another possibility is :

for i in * ; do echo -n $i" : "  ; head -n 27 "$i" | tail -n 1 ; done > output.txt