3

I am trying to find duplicates on a disk containing all my pictures. for this purpose I created a file containing potential duplicates (using some exif and check sum properties but this is not the purpose of the question).

I have created a file using this format (using exiftool mainly and little formatting):

./PICTURES_archives/a organiser/Ipad/823WGTMA/IMG_1777.JPG <--> ./PICTURES_archives/a organiser/Ipad/965YOKDJ/IMG_2346.JPG

./PICTURES_archives/a organiser/iCloud Photos/My Photo Stream/IMG_0954.JPG <--> ./Pictures A classer/Iphone 5S Icloud/IMG_0954.JPG

I created the following awk script to show the same output in a different format:

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

More readable to me. the output is :

file a common path file b
. .
PICTURES_archives
a organiser
Ipad
823WGTMA 965YOKDJ
IMG_1777.JPG IMG_2346.JPG
. .
PICTURES_archives Pictures A classer
a organiser Iphone 5S Icloud
iCloud Photos IMG_0954.JPG
My Photo Stream
IMG_0954.JPG

QUESTION : I would like to put color for the output when the information for file a and b are different.

I tried to end the function with

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

the ESC[33m is not interpreted as color.

Any tips?

OS: Darwin macOS Big Sur

cas
  • 78,579
Ajo
  • 83
  • 6
  • 5
    Exactly what shows you the output? Those colour codes only work on the terminal (and they vary by terminal type) -- redirecting to a file will contain the octal data itself. Where does "ESC" come from as a 3-letter string? Commands like od show ascii values in a spaced-out format -- I don't know anything that would render precisely "ESC[". – Paul_Pedant Jul 12 '21 at 11:57
  • 1
    What are you actually trying to do? i.e. what is the original problem you're trying to solve that made you decide that file format with <--> as the separator was a good way to do it? This looks like an XY Problem to me. – cas Jul 12 '21 at 12:51
  • @Paul_Pedant that's how less will show them if you don't use -R. Try printf "\033[33m foo \033[0m\n" | \less. Ajo, is that what you are using? Does using less -R solve it for you or do you need more? – terdon Jul 12 '21 at 12:55
  • Please [edit] your question to provided minimal, complete, concise, testable, plain text sample input and expected output that demonstrates your problem and which we can copy/paste to test a potential solution against so we can help you. – Ed Morton Jul 12 '21 at 13:17
  • If you're just trying to color some awk output then see https://stackoverflow.com/a/64046525/1745001 for one way to produce output with foreground and/or background coloring. – Ed Morton Jul 12 '21 at 13:20
  • @terdon Thanks. It would never occur to me to colourise text and then pipe it through another command. Or indeed to put it in an email or file, when the terminal escapes relate only to the terminal it ran on initially. – Paul_Pedant Jul 12 '21 at 14:11
  • @Paul_Pedant so many people do it and then they post here "how do I remove all these weird characters?". Frequently it's caused by them having ls aliased to ls -C or grep aliased to grep --color and then they forget they did that or what exactly that implies to any subsequent processing! – Ed Morton Jul 12 '21 at 17:03
  • @terdon you got it. This was the issue. Having the less -R solves the problem. Makes it clearer for me. The script is correct and the less is the issue. Thank toi all for your contributions. – Ajo Jul 12 '21 at 19:39
  • @Cas. Thanks for the edit. My question became more readable. – Ajo Jul 12 '21 at 20:06
  • @Paul_Pedant I do it all the time. I even have a script to colorise what I'm looking for. I won't save to a file, of course, but it is often useful to pipe long output to a pager like less so I can scroll through quickly and have the relevant lines (colored ones) jump out at me. – terdon Jul 13 '21 at 18:04
  • @terdon But you know exactly what you are doing! I have had colleagues piping colourised text to mailx and expecting a web mail viewer to deal with xterm escapes. Also, some commands only colour when stdout is a tty -- a file or pipe gets different (plain) text. (Whoops: checking ls -l | more, found a file called 'k'$'\033''q'). – Paul_Pedant Jul 14 '21 at 08:29

1 Answers1

3

The problem was that I was piping the output to less and less doesn't interpret these escape sequences by default. However, it works fine if I use less -R. This is documented in 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.
terdon
  • 242,166
Ajo
  • 83
  • 6
  • Thanks for posting an answer! Since it was based on my comment, I took the liberty of removing your kind attribution and adding some context and the relevant section of the man page to flesh it out a little. – terdon Jul 12 '21 at 21:46
  • Thank you. Nicely done. I will be able to close the subject in few hours. – Ajo Jul 13 '21 at 17:58