How do you pipe input through grep to another utility?

前端 未结 3 1973
北海茫月
北海茫月 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("")}'
    

提交回复
热议问题