awk颜色输出

awk颜色输出

我试图在包含我所有图片的磁盘上查找重复项。为此,我创建了一个包含潜在重复项的文件(使用一些 exif 和校验和属性,但这不是问题的目的)。

我使用这种格式创建了一个文件(主要使用 exiftool 和少量格式):

./PICTURES_archives/组织者/Ipad/823WGTMA/IMG_1777.JPG <--> ./PICTURES_archives/组织者/Ipad/965YOKDJ/IMG_2346.JPG

./PICTURES_archives/a Organizer/iCloud Photos/我的照片流/IMG_0954.JPG <--> ./Pictures A classer/Iphone 5S Icloud/IMG_0954.JPG

我创建了以下awk脚本以不同的格式显示相同的输出:

awk -F'<-->' 'BEGIN {
                format1= "%25s %-50s\n"; 
                format2 = "%-50s %s\n";
                compt=1 
              } 
              {
                compt++; 
                split($1,a,"/"); 
                split($2,b,"/"); 
                longb=length(b);
                longa=length(a); 
                long=longb; 
                if (longa>longb) long=longa; 
                for(i=1; i<=long;i++) {
                    if(a[i]==b[i]) printf format1,"    ",  a[i] ; 
                    else printf format2, a[i],b[i]
                } 
                print "\n"
              }' identical.txt 

对我来说更具可读性。输出是:

归档一个 共同路径 文件b
图片_档案
组织者
平板电脑
823WGTMA 965YOKDJ
IMG_1777.JPG IMG_2346.JPG
图片_档案 图片 A Classer
组织者 iPhone 5S 云盘
iCloud 照片 IMG_0954.JPG
我的照片流
IMG_0954.JPG

问题:当文件 a 和 b 的信息不同时,我想为输出添加颜色。

我试图用以下方式结束该功能

printf format2, "\033[33m"a[i] "\033[0m","\033[33m"b[i] "\033[0m"

but it shows me the following output

ESC[33m823WGTMAESC[0m                                  ESC[33m965YOKDJESC[0m
ESC[33mIMG_1777.JPG ESC[0m                             ESC[33mIMG_2346.JPGESC[0m

ESC[33m 不被解释为颜色。

有小费吗?

操作系统:Darwin macOS Big Sur

答案1

问题是我将输出通过管道传输到less并且less默认情况下不解释这些转义序列。但是,如果我使用它,效果很好less -R。这记录在man less

       -R or --RAW-CONTROL-CHARS
              Like -r, but only ANSI "color" escape sequences and OSC  8  hy‐
              perlink  sequences  are  output  in "raw" form.  Unlike -r, the
              screen appearance is maintained correctly, provided that  there
              are  no  escape sequences in the file other than these types of
              escape sequences.  Color escape sequences  are  only  supported
              when  the  color  is changed within one line, not across lines.
              In other words, the beginning of each line  is  assumed  to  be
              normal  (non-colored),  regardless  of  any escape sequences in
              previous lines.  For the purpose of keeping track of screen ap‐
              pearance,  these  escape  sequences are assumed to not move the
              cursor.

相关内容