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

后端 未结 2 666
悲哀的现实
悲哀的现实 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:40

    An alternative to using awk functions is passing the colors in shell variables. E.g.

    RED='\033[01;31m'
    GREEN='\033[01;32m'
    YELLOW='\033[01;33m'
    BLUE='\033[01;34m'
    NONE='\033[0m'
    
    echo "Col1 Col2 Col3 Col4" | \
    awk -v r=$RED -v y=$YELLOW -v g=$GREEN -v b=$BLUE -v n=$NONE \
     '{printf r$1n y$2n g$3n b$4n "\n"}'
    

提交回复
热议问题