How to print awk's results with different colors for different fields?

后端 未结 2 678
悲哀的现实
悲哀的现实 2020-12-29 06:51

This file has 3 fields. I wanted e.g. the first 2 fields in green, and the third in white (NB : black background), so I tried :

awk \'{print \"\\033[0;32m\"$         


        
2条回答
  •  独厮守ぢ
    2020-12-29 07:39

    To get color output from awk, you can use this approach.

    function red(s) {
        printf "\033[1;31m" s "\033[0m "
    }
    
    function green(s) {
        printf "\033[1;32m" s "\033[0m "
    }
    
    function blue(s) {
        printf "\033[1;34m" s "\033[0m "
    }
    
    {
        print red($1), green($2), blue($3)
    }
    

提交回复
热议问题