I am a Unix Newbie and have been trying to get around this one task for quite some time now. Would be great if someone could help me.
Problem - I need to loop through a folder, into each .txt file and then fetch the last 5 lines that have a specific word (ex : test)
I am trying to traverse through the folder, into each file, and print the last 5 lines where the word 'test' has occured.
for example - if the word 'test' is found in line numbers 1,3,5,7,9,12,14,15. then I need to only print the whole line at line number 7,9,12,14,15.(i.e the last 5 lines that have the occurrence of this word)
I am somehow not being able to upload images in my question but I hope this info helps: File 1 - [Here there are 7 occurances of the word test and I need just the last 5] File 2 - [Here there are 6 occurances of the word test and I need just the last 5]
The command I figured out is : cd to folder path:
find . -type f -exec grep -iH "test" {} + | tail -n5; tail -n0 | grep -i test
This command works but it prints the last 5 lines and then exits. It does not go into another file.(since it already has found its last 5 lines with the matching word)
In my case, however I want the output to be the last 5 lines of 'each' file.
Can someone please help. Thank you in advance.