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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i use tail & grep in real time here??
# 1  
Old 04-24-2013
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
# 2  
Old 04-24-2013
What if you forget about the tail and just periodically look for the word?
Code:
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

# 3  
Old 04-24-2013
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.
# 4  
Old 04-24-2013
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
Code:
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)
# 5  
Old 04-24-2013
Yes, it will run forever. Here is another possibility if you want to stick with the tail command (if your grep supports -m option).
Code:
$ 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

# 6  
Old 04-24-2013
If you want to check only the portions of the file added since your last visit, you might be inspired by this post.
# 7  
Old 04-24-2013
Something wrong with the link, I think...
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting real time to epoch time

# date +%s -d "Mon Feb 11 02:26:04" 1360567564 # perl -e 'print scalar localtime(1360567564), "\n";' Mon Feb 11 02:26:04 2013 the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input 1359453135154 rather than 10 digit epoch time 1360567564... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

How to tail real time file Generated every hour?

Hi Guys, I am developing a script to monitor log file Generated every hour. When creating a new file scripts not working. My Code . I want to monitor GatewayTransaction.yyyymmdd-hh.log while true; do newdate=$(date '+%Y%m%d') ; nowdate=$(date '+%Y%m%d-%H.log'); tail -f... (7 Replies)
Discussion started by: ooilinlove
7 Replies

3. UNIX Desktop Questions & Answers

grep a range of time & date

how can i grep a range? i have a text file with the following text: result.log.00:2012/01/02 12:00:07.422 LOG STARTED HERE N6Kashya29MemoryShieldScheduler_AO_IMPLE, pid=8662/8658, config=(alertThreshold=10,alertLevel=0,killThreshold=7200,coreThreshold=0,full=1), deltaTime=0,... (1 Reply)
Discussion started by: boaz733
1 Replies

4. Shell Programming and Scripting

How to tail -f real time file.

How to tail -f real time file. I want to tail file created last time. The server is gen new file Always. . An example file. -rw-r--r-- 1 shinnie tiituck 251M Oct 18 05:39 20111018_00.log -rw-r--r-- 1 shinnie tiituck 251M Oct 18 11:18 20111018_01.log -rw-r--r-- 1 shinnie tiituck... (3 Replies)
Discussion started by: ooilinlove
3 Replies

5. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

6. Shell Programming and Scripting

grep - date & time range

Hi, I need to search email files by date & time range in email files. The timezone is not important. Can someone plz advise how i can do this ? For e.g A user can specify only A single date A date range date & time range Below is part of the email file. (4 Replies)
Discussion started by: coolatt
4 Replies

7. Shell Programming and Scripting

Show date/time with tail|grep command

Hi, I have a log file without date/time, and I want that everytime tail|grep find something it displays the date/time and the line. I have tried something like this command but without any luck to display the date/time: tail -F catalina.out | sed "s/^/`date `/" | egrep ... (6 Replies)
Discussion started by: julugu
6 Replies
Login or Register to Ask a Question