Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

shell - get last line from grep search on multiple files

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
for f in $(find . -name "FILE_NAME"); do grep PATTERN $f | tail -1; done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...