LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2012, 04:03 AM   #1
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 67

Rep: Reputation: 8
tail and grep


Hello!
I've used the command tail together with grep, and I don't quite understand why it works the way it does.

I'm using the command
Code:
tail /var/log/messages
and it shows the last 10 lines -> OK

I'm using the command
Code:
tail -f /var/log/messages
and it shows the last 10 lines and will update -> OK

I'm using
Code:
tail /var/log/messages | grep -v xxx | grep -v yyy
and it shows the remainder of the last 10 lines which don't contain xxx or yyy

I'm using
Code:
tail -f /var/log/messages | grep -v xxx | grep -v yyy
and it doesn't show the remainder of the last 10 lines...

I've checked, the last lines haven't changed and there are lines which will be shown if I leave off the -f.

Why don't they show up with the -f?
 
Old 09-13-2012, 04:11 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Most likely because you have to specifically tell grep to read from a stream of data. This is accomplished using the --line-buffered option, e.g.
Code:
tail -f /var/log/messages | grep --line-buffered -v xxx | grep -v yyy
As you can see, it is necessary only for the first grep, which is the command that receives the piped input from tail. Hope this helps.
 
1 members found this post helpful.
Old 09-13-2012, 04:26 AM   #3
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 67

Original Poster
Rep: Reputation: 8
That's it. Thank you.
 
Old 09-13-2012, 04:28 AM   #4
vp0619520
Member
 
Registered: Jan 2012
Posts: 55

Rep: Reputation: Disabled
Hi,I just took a test.I found that too.I don know why...But I think the command below can help you
Code:
tail -f /var/log/messages |grep -Ev [xxx,yyy]
 
Old 09-13-2012, 04:45 AM   #5
KatrinAlec
Member
 
Registered: Feb 2012
Posts: 67

Original Poster
Rep: Reputation: 8
@colucix: I've tried some more and If I use grep three times I have to use --line-buffered on the first two, to make it work the way I want it to. That's fine by me.

@vp0619520: -E seems to work with single characters, but I want to match a word.
I'll have to research how extended regluar expressions work first, but doing that can definitely be an option.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
[SOLVED] tail and grep issue Wim Sturkenboom Linux - General 6 02-06-2008 07:19 AM
how to use grep and tail to get portion of a file laurasong Linux - General 7 08-19-2007 11:59 PM
Weird tail + grep bug Aeiri Linux - Software 2 12-15-2006 05:30 AM
Help pls!!! grep or tail->restart poj Linux - Software 3 11-26-2004 12:50 AM


All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration