LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   how to find a word in a textfile starting from the BOTTOM of the textfile ? (http://www.linuxquestions.org/questions/linux-software-2/how-to-find-a-word-in-a-textfile-starting-from-the-bottom-of-the-textfile-787731/)

markraem 02-08-2010 04:54 AM

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

kbp 02-08-2010 05:03 AM

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

markraem 02-08-2010 05:56 AM

kbp,
thanks for your input, works OK for me.

jschiwal 02-08-2010 06:12 AM

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.


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