Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pipe input through grep to another utility?

I am using 'tail -f' to follow a log file as it's updated; next I pipe the output of that to grep to show only the lines containing a search term ("org.springframework" in this case); finally I'd like to make is piping the output from grep to a third command, 'cut':

tail -f logfile | grep org.springframework | cut -c 25- 

The cut command would remove the first 25 characters of each line for me if it could get the input from grep! (It works as expected if I eliminate 'grep' from the chain.)

I'm using cygwin with bash.

Actual results: When I add the second pipe to connect to the 'cut' command, the result is that it hangs, as if it's waiting for input (in case you were wondering).

like image 420
les2 Avatar asked Jun 09 '09 20:06

les2


People also ask

Can you pipe grep?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

How do you pipe the output of a command to another command?

The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right. # First, echo "Hello World" will send Hello World to the standard output.

How do you pipe a command in Linux?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

How do you chain grep commands?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.


2 Answers

Assuming GNU grep, add --line-buffered to your command line, eg.

tail -f logfile | grep --line-buffered org.springframework | cut -c 25- 

Edit:

I see grep buffering isn't the only problem here, as cut doesn't allow linewise buffering.

you might want to try replacing it with something you can control, such as sed:

tail -f logfile | sed -u -n -e '/org\.springframework/ s/\(.\{0,25\}\).*$/\1/p' 

or awk

tail -f logfile | awk '/org\.springframework/ {print substr($0, 0, 25);fflush("")}' 
like image 84
Hasturkun Avatar answered Oct 13 '22 19:10

Hasturkun


On my system, about 8K was buffered before I got any output. This sequence worked to follow the file immediately:

tail -f logfile | while read line ; do echo "$line"| grep 'org.springframework'|cut -c 25- ; done 
like image 41
Dennis Williamson Avatar answered Oct 13 '22 18:10

Dennis Williamson