When given just the --delete (-d) option, tr
removes any input characters that are in array1.
When given just the --squeeze-repeats (-s) option
and not translating, tr
replaces each input sequence of a
repeated character that is in array1 with a single occurrence of
that character.
When given both --delete and --squeeze-repeats, tr
first performs any deletions using array1, then squeezes repeats
from any remaining characters using array2.
The --squeeze-repeats option may also be used when translating,
in which case tr
first performs translation, then squeezes
repeats from any remaining characters using array2.
Here are some examples to illustrate various combinations of options:
tr -d '\0'
tr -cs '[:alnum:]' '[\n*]'
tr -s '\n'
uniq
with the -d option to print out only the words
that were repeated.
#!/bin/sh cat -- "$@" \ | tr -s '[:punct:][:blank:]' '[\n*]' \ | tr '[:upper:]' '[:lower:]' \ | uniq -d
tr -d axM
However, when ‘-’ is one of those characters, it can be tricky because
‘-’ has special meanings. Performing the same task as above but also
removing all ‘-’ characters, we might try tr -d -axM
, but
that would fail because tr
would try to interpret -a as
a command-line option. Alternatively, we could try putting the hyphen
inside the string, tr -d a-xM
, but that wouldn’t work either because
it would make tr
interpret a-x
as the range of characters
‘a’…‘x’ rather than the three.
One way to solve the problem is to put the hyphen at the end of the list
of characters:
tr -d axM-
Or you can use ‘--’ to terminate option processing:
tr -d -- -axM