LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-11-2004, 11:34 AM   #1
ericcarlson
Member
 
Registered: Jan 2002
Posts: 162

Rep: Reputation: 30
Need to search for *last* occurrance in a text file


Hi, I need to run a grep or whatever that returns the last line on which the target string appears, and returns it to a variable in a bash script.
Something like:

last= grep "target" myfile.txt

I know thats nothing like the right syntax but thats why I'm asking!
The target will appear many times in the file, I must get the last one only. Thanks!
 
Old 09-11-2004, 02:08 PM   #2
n0ne
LQ Newbie
 
Registered: Aug 2003
Distribution: Fedora Core 2, Debian Sarge
Posts: 5

Rep: Reputation: 0
# MyVariable=`grep "target" myfile.txt | tail -n1`

Exemple
# cat toto.txt
1 toto
2 bob
3 titi
4 tata
5 bob
6 toto

# MyVariable=`grep bob toto.txt | tail -n1`
# echo $MyVariable
5 bob
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which command to search text/phrase in file? b:z Linux - General 2 03-30-2005 09:11 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM
How do you search for a file in text mode? cyberkid12 Linux - General 7 12-21-2002 12:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration