Monday, November 18, 2019

How to 'grep' a continuous stream?

How to 'grep' a continuous stream?

EmailThis Premium lets you save unlimited bookmarks, PDF, DOCX files, PPTs and images. It also gives you a PDF copy of every page that you save. Upgrade to Premium →

Is that possible to use grep on a continuous stream?

What I mean is sort of a tail -f <file> command, but with grep on the output in order to keep only the lines that interest me.

I've tried tail -f <file> | grep pattern but it seems that grep can only be executed once tail finishes, that is to say never.

Marcin

2,58211 gold badge1818 silver badges4242 bronze badges

asked Aug 23 '11 at 13:34

Matthieu NapoliMatthieu Napoli

37.8k3232 gold badges143143 silver badges226226 bronze badges

Turn on grep's line buffering mode when using BSD grep (FreeBSD, Mac OS X etc.)

tail -f file | grep --line-buffered my_pattern

You don't need to do this for GNU grep (used on pretty much any Linux) as it will flush by default (YMMV for other Unix-likes such as SmartOS, AIX or QNX).

Wes Mason

1,4131111 silver badges1010 bronze badges

answered Aug 23 '11 at 14:44

tadtad

12.7k22 gold badges1010 silver badges22 bronze badges

I use the tail -f <file> | grep <pattern> all the time.

It will wait till grep flushes, not till it finishes (I'm using Ubuntu).

answered Aug 23 '11 at 13:37

Irit KatrielIrit Katriel

2,94111 gold badge1212 silver badges1818 bronze badges

I think that your problem is that grep uses some output buffering. Try

tail -f file | stdbuf -o0 grep my_pattern

it will set output buffering mode of grep to unbuffered.

answered Aug 23 '11 at 13:58

XzKtoXzKto

2,1421313 silver badges1717 bronze badges

If you want to find matches in the entire file (not just the tail), and you want it to sit and wait for any new matches, this works nicely:

tail -c +0 -f <file> | grep --line-buffered <pattern>

The -c +0 flag says that the output should start 0 bytes (-c) from the beginning (+) of the file.

answered Sep 11 '17 at 22:34

Ken WilliamsKen Williams

17.2k55 gold badges6161 silver badges109109 bronze badges

In most cases, you can tail -f /var/log/some.log |grep foo and it will work just fine.

If you need to use multiple greps on a running log file and you find that you get no output, you may need to stick the --line-buffered switch into your middle grep(s), like so:

tail -f /var/log/some.log | grep --line-buffered foo | grep bar

answered May 4 '16 at 17:14

Dale AndersonDale Anderson

1,1361515 silver badges1717 bronze badges

Didn't see anyone offer my usual go-to for this:

less +F <file> ctrl + c /<search term> <enter> shift + f

I prefer this, because you can use ctrl + c to stop and navigate through the file whenever, and then just hit shift + f to return to the live, streaming search.

answered Apr 10 '18 at 2:20

you may consider this answer as enhancement .. usually I am using

tail -F <fileName> | grep --line-buffered  <pattern> -A 3 -B 5

-F is better in case of file rotate (-f will not work properly if file rotated)

-A and -B is useful to get lines just before and after the pattern occurrence .. these blocks will appeared between dashed line separators

But For me I prefer doing the following

tail -F <file> | less

this is very useful if you want to search inside streamed logs. I mean go back and forward and look deeply

answered Jul 27 '16 at 23:39

mebadamebada

1,75544 gold badges2121 silver badges3434 bronze badges

sed would be a better choice (stream editor)

tail -n0 -f <file> | sed -n '/search string/p'

and then if you wanted the tail command to exit once you found a particular string:

tail --pid=$(($BASHPID+1)) -n0 -f <file> | sed -n '/search string/{p; q}'

Obviously a bashism: $BASHPID will be the process id of the tail command. The sed command is next after tail in the pipe, so the sed process id will be $BASHPID+1.

answered Apr 21 '17 at 16:14

Yes, this will actually work just fine. Grep and most Unix commands operate on streams one line at a time. Each line that comes out of tail will be analyzed and passed on if it matches.

answered Aug 23 '11 at 13:41

CalebCaleb

3,70811 gold badge3030 silver badges4848 bronze badges

This one command workes for me (Suse):

mail-srv:/var/log # tail -f /var/log/mail.info |grep --line-buffered LOGIN  >> logins_to_mail

collecting logins to mail service

answered Oct 31 '18 at 7:02

Use awk(another great bash utility) instead of grep where you dont have the line buffered option! It will continuously stream your data from tail.

this is how you use grep

tail -f <file> | grep pattern

This is how you would use awk

tail -f <file> | awk '/pattern/{print $0}'

answered Jun 22 '14 at 13:39

AtifAtif

2922 bronze badges

Not the answer you're looking for? Browse other questions tagged linux bash shell grep tail or ask your own question.

Source: https://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream

Please check the attached file.

EmailThis was not able to extract useful content from the website. Hence, we have saved the webpage to a PDF file. You can find that attached along with this email.

Upgrade to Premium Plan

✔ Save unlimited bookmarks.

✔ Save PDFs, DOCX files, images and Excel sheets as email attachments.

✔ Get priority support and access to latest features.

Upgrade to Premium

No comments: