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
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
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.