Color regex matches - without dropping misses

后端 未结 7 911
日久生厌
日久生厌 2021-02-07 02:21

When using grep --color=always I can get pretty color highlighting for regex matches.

However, grep only returns lines with at least one match.

7条回答
  •  执念已碎
    2021-02-07 02:55

    I recently made something similar as a filter. I use it to color the "headers" in a tail with multiple files, like this:

    tail -f access.log error.log foo.log | logcol.sh

    The headers look like this:

    ==> access.log <==

    I got confused by the quick changes between the different logfiles, so this logcol.sh helps. The ==> is hardcoded for the specific usage but could be a parameter as well.

    #!/bin/sh
    while read line
    do
      if test `expr "$line" : "==>.*"`  -eq 0 ;
      then
        printf '\033[0m%s\n' "$line"
      else
        printf '\033[0;31m%s\n' "$line"
      fi
    done
    

    Maybe not the most elegant but I think it's quite readable. I hope I don't have any typos ;-) HTH, rob

提交回复
热议问题