How can I view log files in Linux and apply custom filters while viewing?

前端 未结 5 1010
慢半拍i
慢半拍i 2021-01-30 12:45

I need to read through some gigantic log files on a Linux system. There\'s a lot of clutter in the logs. At the moment I\'m doing something like this:

cat logf         


        
5条回答
  •  被撕碎了的回忆
    2021-01-30 13:31

    Based on ghostdog74's answer and the less manpage, I came up with this:

    ~/.bashrc:

    export LESSOPEN='|~/less-filter.sh %s'
    export LESS=-R  # to allow ANSI colors
    

    ~/less-filter.sh:

    #!/bin/sh
    case "$1" in
    *logfile*.log*) ~/less-filter.sed < $1
      ;;
    esac
    

    ~/less-filter.sed:

    /deleteLinesLikeThis/d  # to filter out lines
    s/this/that/  # to change text on lines (useful to colorize using ANSI escapes)
    

    Then:

    • less logfileFooBar.log.1 -- applies the filter applies automatically.
    • cat logfileFooBar.log.1 | less -- to see the log without filtering

    This is adequate for now but I would still like to be able to edit the filters on the fly.

提交回复
热议问题