Previous: Translating, Up: tr invocation [Contents][Index]
When given just the --delete (-d) option, tr
removes any input characters that are in set1.
When given just the --squeeze-repeats (-s) option
and not translating, tr
replaces each input sequence of a
repeated character that is in set1 with a single occurrence of
that character.
When given both --delete and --squeeze-repeats, tr
first performs any deletions using set1, then squeezes repeats
from any remaining characters using set2.
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 set2.
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
More generally, use the character class notation [=c=]
with ‘-’ (or any other character) in place of the ‘c’:
tr -d '[=-=]axM'
Note how single quotes are used in the above example to protect the square brackets from interpretation by a shell.
Previous: Translating, Up: tr invocation [Contents][Index]