LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2015, 10:27 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
tail -f with multiple pipes


Code:
$ sudo tail -f /var/log/messages | grep LOGPREFIX | awk '{print $11}'
I am watching some iptables logging and want to filter it
I want to look from any strings containing LOGPREFIX and then print the 11th column.
However, when I run the above, it sits there without printing anything. Removing the awk command causes it to display the output. Removing the -f (to actively follow it) causes it to work with the awk command.

Is there a limitation in tail to prevent more then one filtering program when using -f or am I writing it wrong?

My output:

Code:
$ sudo tail -f /var/log/messages | grep LOGPREFIX
Jul  1 20:14:25 HOSTNAME kernel: [1245137.142033] LOGPREFIX IN=eth1 OUT= MAC=macaddress SRC=source_ip DST=dest_ip LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=59154 DF PROTO=TCP SPT=43935 DPT=7778 WINDOW=29200 RES=0x00 SYN URGP=0
Jul  1 20:14:25 HOSTNAME kernel: [1245137.142033] LOGPREFIX IN=eth1 OUT= MAC=macaddress SRC=source_ip DST=dest_ip LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=59154 DF PROTO=TCP SPT=43935 DPT=7778 WINDOW=29200 RES=0x00 SYN URGP=0
...
Code:
$ sudo tail -f /var/log/messages | grep LOGPREFIX | awk '{print $11}'
_ (no output)
Code:
$ sudo tail /var/log/messages | grep LOGPREFIX | awk '{print $11}'
SRC=dest_ip
SRC=dest_ip
...

Last edited by Sefyir; 07-01-2015 at 10:40 PM.
 
Old 07-01-2015, 10:29 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Your problem is that grep is block-buffering its output because its stdout is not a terminal. Try "grep --line-buffered LOGPREFIX".
 
1 members found this post helpful.
Old 07-01-2015, 10:33 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Or better yet, get rid of the grep altogether and use awk to do the filtering as well as the print.
 
Old 07-01-2015, 10:40 PM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Original Poster
Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Quote:
Originally Posted by rknichols View Post
Your problem is that grep is block-buffering its output because its stdout is not a terminal. Try "grep --line-buffered LOGPREFIX".
Thank you! This worked perfectly.

Quote:
Or better yet, get rid of the grep altogether and use awk to do the filtering as well as the print.
I'm not familiar with awk, I've only recently started to use it for column filtering. How would I use awk in this situation to remove the need for grep?
 
Old 07-02-2015, 12:03 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Code:
sudo tail /var/log/messages | awk '/LOGPREFIX/ {print $11}'
This is a short form of "$0 ~ /LOGPREFIX/" - does the input record contain the regex (in this case a regex constant). Note is is case sensitive.

Last edited by syg00; 07-02-2015 at 12:06 AM. Reason: forgot "tail"
 
1 members found this post helpful.
  


Reply



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
tee command - multiple pipes laki47 Linux - Newbie 1 02-17-2012 09:31 AM
[SOLVED] ssh into multiple servers and tail -f greenpool Linux - Newbie 7 09-28-2011 11:05 PM
Multiple pipes with recursion ambika_29 Programming 4 12-02-2010 02:47 PM
C - Using pipes with a parent that has multiple children golmschenk Programming 5 03-07-2010 03:30 PM
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 AM.

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
Open Source Consulting | Domain Registration