I need to write a small shell script which does the following :
I have a file : root/var/log/ocmp/ocmpclient.log
This is a log file which is continuosly getting updated . I have to keep looking into this file all the time. I have to look for four keywords, "File Detected", File Sending", "File Recieved", "Disconnecting"
Now whenever i find one of these keywords i have to print them on the screen, this process continues till the computer shuts down.
I am using the above command now i want to write the output of this to go to a file output.txt
so i modified it as
#!/bin/sh
while true
do
tail -f /root/var/log/ocmp/ocmpclient.log | grep 'File Detected\|File Sending\|File Recieved\|Disconnecting' > output.txt
done
Now the problem is the file is created but doesnt contain anything.
You don't need a while loop: "tail -f" is already an infinite loop until you manually terminate the process.
If you try:
tail -f file.log | grep "something" > output.txt
Your output file will remain empty until the grep buffer is flushed, which happens every "tot" bytes. If your grep version supports the option "--line-buffered" use it and you're done, otherwise you may need a while loop like:
tail -f file.log | while read LINE
do
echo "$LINE" | grep "something" >> output.txt
done