How do you pipe input through grep to another utility?

前端 未结 3 1959
北海茫月
北海茫月 2020-12-08 14:29

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\"

相关标签:
3条回答
  • 2020-12-08 15:01

    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("")}'
    
    0 讨论(0)
  • 2020-12-08 15:04

    What you have should work fine -- that's the whole idea of pipelines. The only problem I see is that, in the version of cut I have (GNU coreutiles 6.10), you should use the syntax cut -c 25- (i.e. use a minus sign instead of a plus sign) to remove the first 24 characters.

    You're also searching for different patterns in your two examples, in case that's relevant.

    0 讨论(0)
  • 2020-12-08 15:06

    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
    
    0 讨论(0)
提交回复
热议问题