LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 02-08-2010, 05:54 AM   #1
markraem
Member
 
Registered: Nov 2003
Posts: 82

Rep: Reputation: 15
how to find a word in a textfile starting from the BOTTOM of the textfile ?


Hi LQ,

I have a text file example :
word1
word2
word4
lookstring
word4
word3
word3
lookstring
word11
word21
word22



I would like to
extract the part of that file that contains
lookstring
word11
word21
word22

It means I have to look for the lookstring word from the bottom and maybe with sed take the rest of file.

Any ideas how i can do that ?
Any ideas how I can search a string from the bottom of a text file ?

Thanks in adavance
 
Old 02-08-2010, 06:03 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
You can process a file from end to start using 'tac' ( cat backwards )

<edit>Here's a l33t script that took me at least 2 minutes:
Code:
#!/bin/bash

REGEX=lookstring
FILE=$1

for line in $(tac $FILE)
do
	echo $line >> tmpfile
	if [ ! -z "$(echo $line | grep $REGEX)" ]
	then
		break
	fi
done

tac tmpfile > outfile
rm tmpfile
</edit>

cheers

Last edited by kbp; 02-08-2010 at 06:17 AM.
 
Old 02-08-2010, 06:56 AM   #3
markraem
Member
 
Registered: Nov 2003
Posts: 82

Original Poster
Rep: Reputation: 15
kbp,
thanks for your input, works OK for me.
 
Old 02-08-2010, 07:12 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
If this is homework, I would recommend reading through the info manual for "coreutils". It is the package that supplies most of the text handling utilities. You might even consider downloading the source and producing a pdf version of the manual for reference.

./configure
make pdf

Then copy the manual from the doc/ directory of the source to your Documents directory.
 
  


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
cat to grep to textfile? jabfinger Linux - General 3 09-05-2006 04:25 PM
Encoding of a textfile Anden008 Linux - Newbie 3 11-15-2005 03:07 PM
textfile editing blubbfish Linux - General 9 10-20-2004 03:00 AM
Help improving a textfile blubbfish Linux - Newbie 2 06-22-2004 09:02 AM
PDF to TextFile? Gerardoj Linux - Software 3 09-29-2003 06:00 PM


All times are GMT -5. The time now is 02:10 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration