How do i use tail & grep in real time here??

Hi

I have a file which is updated very frequently.
Where in i wanted to use tail -f command in the script and wanted to grep for a particular word.

But the issue is when i use tail -f filename|grep "word" ...
it will show me blank until the word is found in the real time. if it shows blank it will execute the next line which i don want i want it to wait until the word is found and then perform the task

What if you forget about the tail and just periodically look for the word?

while sleep 60; do
  grep -q word_to_find logfile
  if [ $? -eq 0 ]; then
    echo found word_to_find
    # take action
    # maybe break from while loop
  fi
done

Why should it "show blank"? If run as you posted, it will wait forever, and every now and then print a line containing the "word" to stdout.

Searching for the pattern in the whole file is not a good idea specially when there is not need for that ( this is what I understood from the requirement ).

May be you can replace grep command ( in hanson44's post) with

tail logfile | grep -q word_to_find

to search in last 10 lines only after some interval ( you can set the interval based on how frequent your file is being updated)

Yes, it will run forever. Here is another possibility if you want to stick with the tail command (if your grep supports -m option).

$ tail -f logfile | grep -m 1 word_to_find
# nothing happens
# in another window, run following:
$ echo word_to_find >> logfile
# grep finds and prints word_to_find
# in another window, run following:
$ echo something_else >> logfile
# grep exits, and tail command ends
# shell is ready to execute next command

If you want to check only the portions of the file added since your last visit, you might be inspired by this post.

Something wrong with the link, I think...

Yes. sorry, corrected it... try now.

hanson & rudi

I want tof ind the word in real time....
if i use tail -f command i ll be ever stuck over there oly...

Please suggest sum alternate idea

Did you try my grep -m suggestion?

If the log file is part of log rotating do use --follow like this:

tail --follow=name logfile | grep -m 1 word_to_find

it aint helping...

grep -m wil help me in serching the 1st occurence.
I'm not looking for that

Depending on the OS flavour, you might be able to use:-

tail -0f filename | grep expression | while read line
do
   :
   :
   :

0 is a zero.

Some versions of HP-UX don't allow the zero lines option of tail, and there may be other flavours that do that too. I'm not sure why grep would give any output if the expression it not found.

Can you paste in the code you have so far?

Thanks, in advance,
Robin

Then what are you looking for? I thought you wanted to take action when there was an occurence.