tail multiple files and grep the output [closed]

北慕城南 提交于 2019-12-09 08:44:01

问题


I would like to grep a pattern from multiple log files which are being constantly updated by some processes and tail the output of this grep continuosly. Below command doesnt work and I get

  • tail: warning: following standard input indefinitely is ineffective
tail -f  | grep --line-buffered "Search this: " /var/links/proc2/id/myprocess*/Daily/myprocess*.log

Can someone help sort this out?


回答1:


You should have a look at multitail tool (Install using sudo apt-get install multitail)

In short, with multitail, you need to use the --mergeall flag for viewing output of all in one place

multitail --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log  | grep --line-buffered "Search this: " 

You can do the same without using grep

multitail -E "Search this: " --mergeall /var/links/proc2/id/myprocess*/Daily/myprocess*.log  

To view the output individually using multitail, this will give the filename as well.

multitail -E "Search this: " /var/links/proc2/id/myprocess*/Daily/myprocess*.log 



回答2:


the mistake is that you give the files to the grep command and not the tail.

the tail -f needs to get the files as input. try:

tail -f  /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "

to get also the file names (however it will not be like grep output it is):

tail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered -e'^==> .* <==$' -e'Search this: '



回答3:


This is an interesting question and the simple answer should be: Use the prefix switch with tail, but unfortunately this is currently not implemented in most versions of tail.

As I see it, you have two options: adapt the standard tools to the task (see Udys answer) or write your own tool with your favorite scripting/programming language.

Below is one way you could do it with the File::Tail::Multi module for perl. Note that you may need to install the module from CPAN (cpan -i File::Tail::Multi).

Save the following script e.g. mtail to your executable path and make the script executable.

#!/usr/bin/env perl

use File::Tail::Multi;

$| = 1;  # Enable autoflush

$tail = File::Tail::Multi->new(RemoveDuplicate => 0,
                               OutputPrefix    => 'f',
                               Files           => \@ARGV);

while(1) { $tail->read; $tail->print; sleep 2 }

Change OutputPrefix to 'p' if you prefer full path prefixes.

Run it like this:

mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep --line-buffered "Search this: "

You do not need to specify --line-buffered when grep is the last command, so this is sufficient:

mtail /var/links/proc2/id/myprocess*/Daily/myprocess*.log | grep "Search this: "


来源:https://stackoverflow.com/questions/19089266/tail-multiple-files-and-grep-the-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!