It is currently Tue, 07 Dec 2021 02:54:03 GMT



 
Author Message
 How to find the last line that contains a word in file

Hi,

Can anybody tell me how  to find the last line that contains a word in
file in a efficient way?

Thanks in advance,
Jose Luis.



 Tue, 09 Sep 2008 16:27:40 GMT   
 How to find the last line that contains a word in file
On 24 Mar 2006 00:27:40 -0800, jose_luis_fdez_diaz_n...@yahoo.es wrote:
[...]
[...]

< /the/file WORD=whatever awk '
  index($0, ENVIRON["WORD"]) {line = $0; found = 1}
  END {if (found) print line}'

--
Stephane



 Tue, 09 Sep 2008 16:35:48 GMT   
 How to find the last line that contains a word in file

tail -1 <filename> | grep -w "search word"

Regards,

Thobias Vakayil



 Tue, 09 Sep 2008 16:47:48 GMT   
 How to find the last line that contains a word in file
On 24 Mar 2006 08:35:48 GMT, Stephane Chazelas wrote:

You may want to compare with

word=whatever
< /the/file grep -F -- "$word" | tail -n 1

Which may not be slower.

--
Stephane



 Tue, 09 Sep 2008 17:01:36 GMT   
 How to find the last line that contains a word in file

perl -MFile::ReadBackwards -e'
$bw = File::ReadBackwards->new( 'yourfile' )
    or die "Cannot read 'yourfile' $!";
while( defined( $line = $bw->getline ) ) {
    $line =~ /\bword\b/ and print $line and exit;
    }
'

John
--
use Perl;
program
fulfillment



 Tue, 09 Sep 2008 19:23:05 GMT   
 How to find the last line that contains a word in file
On 24 Mar 2006 00:27:40 -0800, "jose_luis_fdez_diaz_n...@yahoo.es" <jose_luis_fdez_diaz_n...@yahoo.es> wrote:

If you have 'tail -r' or 'tac' it is probably most efficient to
use one of them to reverse the file (what you're really doing
is making a stream out of the file but from end to beginning
instead of beginning to end which is actually very useful
for some tasks) and search that;

tail -r <file |sed '/word/q;d'
tac <file     |sed '/word/q;d'

Unfortunately both 'tac' and the '-r' option of 'tail' are
non-standard and therefore not guarenteed to be present
on an arbitrary system. The reason why this would be the
most efficient method is that any decent implementation of
these utilities will start reading the file from the end
and the process will only read as much of the file as is
needed until it is killed by the broken pipe because 'sed'
has found a match....

Otherwise you can use something like;

sed -n '/word/h;$!d;g;/./p'

This reads through the entire file putting any
match of 'word' into the sed hold-space and
if that contains something at the end it prints it.

If that is not good enough consider installing 'tac'
or a 'tail' that supports an '-r', or use 'perl'
or write a program in 'C'...

bestwishesfrom
laura

--
echo moc.12klat@daehriaf_arual|sed 's/\(.\)\(.\),*/\2,\1/g;h;s/,//g;/@t/q;G;D'



 Tue, 09 Sep 2008 21:12:54 GMT   
 
   [ 6 post ] 

Similar Threads

1. How to find if the last line ends with a specified word

2. finding files with words that may be on different lines

3. add one word to line which contains..

4. finding last line of files ...

5. Removing last character from last line in a file

6. get last word of a line

7. 'find' arguments find file containing permission

8. How do you list files not containing a particular word

9. List of files NOT containing word


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software