1837764 Members
3489 Online
110119 Solutions
New Discussion

Re: tail -f

 
SOLVED
Go to solution
Roberto Severo
Advisor

tail -f

Hello,

I'm trying to use the following command:

tail -f log.txt | grep sql >> log2.txt

but the log2.txt file is aways empty, even if a line containing "sql" is stored in log.txt.

Can you help me? Is there other way to do that?

tks.

Roberto
18 REPLIES 18
Pete Randall
Outstanding Contributor

Re: tail -f

Roberto,

What I think you really want to do is:

tail log.txt |grep sql >> log2.txt


The -f option is the follow option.


Pete

Pete
Paula J Frazer-Campbell
Honored Contributor

Re: tail -f

Hi

What does
cat log.txt | grep sql >> log2.txt

give you?

Paula
If you can spell SysAdmin then you is one - anon
Bryan D. Quinn
Respected Contributor
Solution

Re: tail -f

Hello Roberto,

I know this will work:

tail -f log.txt | while read LINE
do
echo $LINE | grep sql >> log2.txt
done

I know it is a bit more than what you had, but it will work.

Hope this helps!
-Bryan
DIPAK KUMAR ROY
Frequent Advisor

Re: tail -f

Try without "-f" option.

tail log.txt |grep sql >>log2.txt

Thanks
Paula J Frazer-Campbell
Honored Contributor

Re: tail -f

Hi
tail -n 10 log.txt |grep sql >> log2.txt

will do the last tem lines.

Paula
If you can spell SysAdmin then you is one - anon
Roberto Severo
Advisor

Re: tail -f

Pete,

What i want is to monitor the file log.txt, and as soon as a line with "sql" is appended, the file "log2.txt" is appended too. In other words, the file "log2.txt" is a online filter of the "log.txt".

Roberto
Helen French
Honored Contributor

Re: tail -f

You may need to try 'cat' command instead of tail:

# cat log.txt | grep sql >> log2.txt

tail command without a line option will list only the last 10 lines, but can increase that number with:

# tail -20 or tail -50 etc
Life is a promise, fulfill it!
Michael Schulte zur Sur
Honored Contributor

Re: tail -f

Hi Roberto,

if you setup a pipe command like you do, output of the grep will not immediately go to log2.txt. It stays in a buffer until enough is ready for output or the command responsible for the output ends and thereby a flush occurs. You should have immediately the text in the outfile, when you quit the command.

greetings,

Michael
Pete Randall
Outstanding Contributor

Re: tail -f

Roberto,

In that case, I think Brian's suggestion is more what you need.


Pete

Pete
Bryan D. Quinn
Respected Contributor

Re: tail -f

Hello Roberto,

I do the same thing with one of our logs and the lines I provided should handle that for you.

-Bryan
DIPAK KUMAR ROY
Frequent Advisor

Re: tail -f

Try this...

while true
do
tail -n 1 log.txt | grep sql >> log2.txt
done



Thanks
Paula J Frazer-Campbell
Honored Contributor

Re: tail -f

Hi

create a file called checksql containing
----------------cut--------------
#!/bin/sh
cat log.txt | grep sql >> log2.txt
sleep 60
exec checksql
----------------cut-----------------

This will pick up all instances of sql and write it out to your log file, sleep for 60 seconds, and then rerun itself.

Paula
If you can spell SysAdmin then you is one - anon
Roberto Severo
Advisor

Re: tail -f

Paula/Dipak,

Your solutions would repeat the past occurrences.

Bryan,

I don't know why, but your solution did not work properly.

Roberto
Michael Schulte zur Sur
Honored Contributor

Re: tail -f

Hi Roberto,

try this:

#!/bin/ksh
LCOUNT=`wc -l log.txt|awk '{print $1}'`
while true
do
LCOUNT2=`wc -l log.txt|awk '{print $1}'`
if test ${LCOUNT} -eq ${LCOUNT2}
then
sleep 5
else
LINENUM=`expr ${LCOUNT2} - ${LCOUNT}`
tail -n${LINENUM} log.txt | grep sql >> log2.txt
LCOUNT=${LCOUNT2}
sleep 5
fi
done
Bryan D. Quinn
Respected Contributor

Re: tail -f

Hello Roberto,

When a line with sql is found in log.txt, then that entire line is appended to log2.txt? Is that correct?

If so, I am not sure why you had problems with my solution. I use it to tail our backup log every night. I just tested it again and as soon as log.txt is updated with a line with sql it is written to log2.txt.

What did you get when you tried it?

-Bryan
Roberto Severo
Advisor

Re: tail -f

Bryan,

I think the problem was my "shell", now everything works fine!

You're 10!

tks
Bryan D. Quinn
Respected Contributor

Re: tail -f

Hello Roberto,

Good, glad it worked out!

Thanks,
-Bryan
Michael Schulte zur Sur
Honored Contributor

Re: tail -f

Hi,

I just found out, that my solution wasnt as waterproof as I thought, but who cares. The solution of Bryan is simple and ingeniuos. I still marvel, why it works. I spent many hours to find something like that because I wanted to controll "interactively" an ftp, but the last line of the output never showed up in the log. So I will have to try this one.

be happy,

Michael