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).

10/19/2015 1:48:51 PM

Accepted Answer

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
6/10/2009 9:12:49 PM

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

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


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("")}'

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow