LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2008, 07:52 AM   #1
fab1234
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Rep: Reputation: 1
Linux scripting search textfile backwards


hey,
i have searched a lot and not found an answer to my problem:
i simply want to find the last occurrence of pattern in a textfile or diplay a textfile backwards. is there an option to cat or ed which does that?
thx for the help
fabian
 
Old 07-10-2008, 07:59 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
if you're already using cat, try tac instead.
 
Old 07-10-2008, 08:32 AM   #3
fab1234
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Original Poster
Rep: Reputation: 1
man your kidding me
....
thanks
 
Old 07-10-2008, 03:53 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Yeah, tac normally gets that kind of response!
 
Old 07-10-2008, 08:17 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
LOL. I hadn't heard of tac either.

Pretty cool. Geek humor
 
Old 07-11-2008, 06:44 AM   #6
fab1234
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Original Poster
Rep: Reputation: 1
yep thats what it is
im pretty new to linux overall so im still finding geekish jokes all the time...
linux itself is a kind of anagram of unix i guess?
 
Old 07-11-2008, 06:45 AM   #7
fab1234
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Original Poster
Rep: Reputation: 1
but back to the originial question:
i used tac | grep now but istnt there a simple option to grep or sed to search backwards?
 
Old 07-11-2008, 07:27 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i don't believe so, it's just such an obscure thing to want to do...
 
Old 07-11-2008, 07:28 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
"linux" is a combination of "Linus" as it the creator Linux torvalds, and "UNIX".
 
Old 07-11-2008, 07:33 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
without using tac, you can also use tail -n num. coupled with a loop to decrement -n backwards and search for your string with grep/awk.
 
Old 07-11-2008, 07:47 AM   #11
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hmm, so tac then right?
 
Old 07-11-2008, 07:58 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
unless on system's without tac.
 
Old 07-11-2008, 09:27 AM   #13
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by ghostdog74 View Post
unless on system's without tac.
Well, in that case we have this
 
Old 07-11-2008, 09:36 AM   #14
fab1234
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Original Poster
Rep: Reputation: 1
well i'd say searching for the last occurence of an event in a logfile isnt so unusual is it...
...or did i miss the irony in your post?

Last edited by fab1234; 07-11-2008 at 09:37 AM.
 
Old 07-11-2008, 09:43 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jiml8 View Post
Well, in that case we have this
and why should i do this when tools such as tail are available? also not every environment allows compiling of source code. That doesn't matter now, since OP has the tac command available.
 
  


Reply



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
search unknown special characters on a textfile bad_jaye Linux - Newbie 6 05-02-2008 09:55 PM
text search and replacement: bash scripting jettachamp26 Programming 35 02-25-2008 06:00 PM
how to search backwards using more just_a_kid Linux - Newbie 4 12-14-2006 05:50 AM
Recursive search in bash scripting ! zulfilee Linux - Software 3 12-12-2004 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 AM.

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