View Full Version : grep
dunryc
August 5th, 2009, 12:25 PM
Hi guys sorry if this sounds like a newb question but it probably is !
does any on know how to use grep to find the last instance of a string in a text file ??
any help would be gratefully appreciated
many thanks dunryc
willjcroz
August 5th, 2009, 12:38 PM
This may work for you:
grep string file | tail -1
or else you can use sed:
sed -n "/string/h;$ {x;p;}" file
Lampi
August 5th, 2009, 12:40 PM
Let's say the text file is called foo.txt and you search dunryc inside of it:
cat <filename> | grep <searchstring>
so this makes it:
cat foo.txt | grep dunryc
dunryc
August 5th, 2009, 12:41 PM
Wow thanks for the quick response ill give them a try and let you know how i get on with them
ahmatti
August 5th, 2009, 12:44 PM
Yeah, these guys beat me to it while I was testing :) You could also do:
tac file | grep -m 1 pattern
This prints out the whole last line, but you can also make grep e.g. to give you the line number.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.