8

Today I learnt a bit about tr command.

But I was stuck at understanding the difference between -c and -C.

On the manual, it said:

 -C      Complement the set of characters in string1, that is ``-C ab'' includes every character except for `a' and `b'.

 -c      Same as -C but complement the set of values in string1.

I'm not quite understand what does set of values in string1 of -c option mean.
I thought it may treat string 1 "ab" as a whole and will escape single a and b.
So I did an experiment:

⇒  echo "ab_a_b" | tr -C 'ba' 'c'
abcacbc%                                                                                                                                                                             
⇒  echo "ab_a_b" | tr -c 'ba' 'c'
abcacbc%

Things didn't match my expectation!
So, what's the difference between -C and -c in tr command?


Software Version: BSD 2004 on OSX10.10

6

The POSIX manual says this:

  • If the -C option is specified, the complements of the characters specified by string1 (the set of all characters in the current character set, as defined by the current setting of LC_CTYPE, except for those actually specified in the string1 operand) shall be placed in the array in ascending collation sequence, as defined by the current setting of LC_COLLATE.

  • If the -c option is specified, the complement of the values specified by string1 shall be placed in the array in ascending order by binary value.

and contains the following note

The ISO POSIX-2:1993 standard had a -c option that behaved similarly to the -C option, but did not supply functionality equivalent to the -c option specified in POSIX.1-2008. This meant that historical practice of being able to specify tr -cd\000-\177 (which would delete all bytes with the top bit set) would have no effect because, in the C locale, bytes with the values octal 200 to octal 377 are not characters.

From this it appears that the -c option let you specify numeric values representing ASCII character instead of using the characters themselves.

  • Auh, I guess because mine was the 2004 BSD version on OSX. – Zen Nov 9 '15 at 3:03
  • But still curious about what does the set of values mean in this early version. – Zen Nov 9 '15 at 3:03
  • @Zen see my edit – casey Nov 9 '15 at 3:12
  • No, they're not the same on BSDs. See there for details. – Stéphane Chazelas Nov 9 '15 at 15:59
3

this question is actually then about BSD tr, not gnu tr.

BSD tr man:

 -C      Complement the set of characters in string1, that is ``-C ab''
         includes every character except for `a' and `b'.

 -c      Same as -C but complement the set of values in string1.

FreeBSD 8.2                    October 13, 2006

GNU tr man:

   -c, -C, --complement
          use the complement of SET1

OSX built in tools will I believe be the BSD versions.

[fixed typo, sed/tr]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.