Friday, October 7, 2022

[SOLVED] get last line from grep search on multiple files

Issue

I'm curently having some problem with a grep command.

I've found the way to only show the last line of a grep search :

grep PATERN FILE_NAME | tail -1

I also find the way to make a grep search in multiple selected files :

find . -name "FILE_NAME" | xargs -I name grep PATERN name

Now I would like to only get the last line of the grep result for each single file. I tried this :

 find . -name "FILE_NAME" | xargs -I name grep PATERN name | tail -1

This returns me only the last value of the last file where I would like to have the last matching patern for every file.


Solution

for f in $(find . -name "FILE_NAME"); do grep PATTERN $f | tail -1; done


Answered By - Daniel Frey
Answer Checked By - Katrina (WPSolving Volunteer)