2

I try to understand what -c option does in tr command according to man tr command it says:

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

But I cannot understad what the doc mean by "complement" so I did the following example to understand it:

I created the following file named trance.txt:

ILOVE YOU
I HATE YOU
i WANNA EAT APPLE PIE

And I run: tr -C A-Za-z "\n" < trance.txt

That gives the output:

ILOVE
YOU
I
HATE
YOU
i
WANNA
EAT
APPLE
PIE

But I still do not get it what actually -c option does. Can you explain to me please?

1

1 Answer 1

4

It replaces the set A-Za-z with its complement, i.e. all the characters in the current character set, minus those specified. Quoting POSIX, in the absence of -d:

  • 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.

So your command is replacing all characters which aren’t A-Z or a-z with newlines.

Because tr uses a one-to-one character map for its replacements, it’s not quite as simple as that; the -c and -C aren’t just “not in” options, they build the set of all characters which aren’t in the given set, in the order specified by the option. This is only relevant if the target set has more than one character.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .