Fastest way to read last line within pattern of multiple files with the same extension

command linefilesscriptingtext processing

I have in a directory /home/files/ some files that all have the same extension .txt.

For example

1.txt
2.txt
3.txt
4.txt
5.txt
test.txt

These files are all the same in data format but the data are different.
All these files have at least the following line inside

frames=[number here]

This line appears multiple times in a file so i need to get the value of frames= that appears last in the file.

And as i stated i want to get that line, from all the above files that match the extension .txt in a directory (no need for recursive).

Is there any single bash command to do that?

EDIT

Expected output:

1.txt=5000
2.txt=123
3.txt=3452
4.txt=111
5.txt=958
test.txt=1231

Best Answer

With find + grep:

find . -name '*.txt' -exec sh -c 'grep -HPo "frames=\K.*" "$0" | tail -n1' {} \;

And with shell for loop + grep in similar fashion:

for file in *.txt; do grep -HPo 'frames=\K.*' "$file" | tail -n1; done
Related Question