Syntax highlighting/colorizing cat

前端 未结 18 1932
不思量自难忘°
不思量自难忘° 2020-12-02 03:39

Is there a method to colorize the output of cat, the way grep does.

For grep, in most consoles it displays a colored output hi

18条回答
  •  时光说笑
    2020-12-02 04:17

    Old question, just answering for the record to provide the solution I ended up using. Perhaps this is a bit hacky (probably not the original intent of the parameter), but:

    alias cgrep='grep -C 9000'

    cat whatever | cgrep 'snozzberries'

    ..grep -C N will provide N lines of context above and below the found item. If it's larger than the input, the whole input is included. In this case, we just make sure it's larger than any typical terminal output we'll want to look at manually, which is generally what you're looking to do if highlighting.

    EDIT : However, this solution suggested below by Beni Cherniavsky-Paskin is superior -- it matches (and highlights) either the word you're looking for or the beginning of the line (not highlightable). The net result is exactly what you want.

    cat whatever | egrep 'snozzberries|$'
    

    That's the new best solution I've seen for that problem, thanks Beni.

提交回复
热议问题