ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, it appears if I pipe two grep together, I don't see the output on stdout. For example
This works
tail -f file.log | grep 'content 1'
This doesn't work
tail -f file.log | grep 'content 1' | grep 'content 2'
Believe the issue is due to buffering by the pipe between the two greps. Now the questions:
1) Why wouldn't the same buffering issue appear between the tail and the first grep?
2) How can I control how much buffering is applied?
The 'tail' program with the "-f" option always line-buffers its output, unlike the usual default of line-buffering only when stdout is connected to a terminal and block-buffering otherwise.
For 'grep', you can use the "--line-buffered" option to make it buffer in the same manner as "tail -f".
The 'tail' program with the "-f" option always line-buffers its output, unlike the usual default of line-buffering only when stdout is connected to a terminal and block-buffering otherwise.
For 'grep', you can use the "--line-buffered" option to make it buffer in the same manner as "tail -f".
You mean the following?
tail -f file.log | grep --line-buffered 'content 1' | grep 'content 2'
Hi, yes it did work. Not only does grep have buffer, the same is also true for awk and sed. Have you run across the unbuffer command? I don't have it on my OS but have seen elsewhere online people are talking about it. Wonder how it compares with the commands own built in switches for turning off buffering.
One thing though, it appears the unbuffer switch for sed (ie sed -u doesn't turn the buffer off completely). I notice that the following:
tail -f logfile.log | egrep --line-unbuffered <filter> | awk '{print{$1} fflush()}' | sed -u -e 's/something//g'
Is sometimes one line behind the following.
tail -f logfile.log | egrep --line-unbuffered <filter>
I'm blaming it on sed because the man page says:
Quote:
-u, --unbuffered
load minimal amounts of data from the input files and flush the output buffers more often
Note it says more often, and not immediately, or perform line buffering.
Hi, yes it did work. Not only does grep have buffer, the same is also true for awk and sed. Have you run across the unbuffer command?
Anything that uses stdio is likely to do similar buffering. The default for stdio is to use line buffering for output to file descriptors connected to a terminal, and block buffering (typically 4K blocks) otherwise, except for stderr, which is always line-buffered. It's easy enough for the program to override the default, but it takes specific action to do so.
Yes, I've seen the 'unbuffer' command. It's part of the 'expect' package, and works by connecting a program's output descriptor to a pseudo-terminal device and then passing the data along to the next stage of the pipeline. When I last tried it several years ago, it didn't work -- output was still block-buffered. I haven't had occasion to try it since.
The issue with 'sed' is that it has an additional layer of internal buffering independent of stdio. You can spend an hour or so reading the manpage about it, but when a look at that manpage I am always reminded of an old comment deep in the source for the kernel's scheduler, "You aren't expected to understand this."
Last edited by rknichols; 10-26-2011 at 11:21 AM.
Reason: Add paragraph about 'sed'
Thanks. Is there a way to switch to line buffering for the PIDs triggered under my current login or a specific PPID? Or to change the buffering size from 4k to 0?
Thanks. Is there a way to switch to line buffering for the PIDs triggered under my current login or a specific PPID? Or to change the buffering size from 4k to 0?
Basically, no. You would have to modify and re-compile each program for which you wanted that change, or else make equivalent changes to the stdio library.
FWIW, some limited testing I just tried with the 'unbuffer' command shows that it seems to be working properly these days. I don't recall just what the issue was with it in the past, though I do know I wasn't the only person having the problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.