Skip to content
Advertisement

How to display a few line from the last few lines for a given number of files?

I have a folder with many different files. I want to display a specific line from the last 100 line of a list of files.

So far I have tried both Grep and Tail but neither gave me exactly what I want:

Example: the folder has the following files:

  • file_1.txt
  • file_2.txt
  • file_3.txt
  • other_file.txt
  • other_file2.txt

Content of file_n.txt is:

  • line 88: test_result: 18209102
  • line 89: good bye
  • ……
  • line 98: test_result: 18202232
  • line 99: good bye

Goal: Only list test_result from last 10 lines of each file_n.txt (line 90 to line 99)

What I have tried so far:

tail -n 10 file_* | grep test_result

The above display almost what I needed but I also need the file name for each result comes from, but for the life of me I can’t figure out how to do it.

Advertisement

Answer

for f in file_*
do
    echo "$f"
    tail -n 10 "$f" | grep test_result
done
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement