tr (COMP2041)

tr is a command-line unix function, used to 'translate' text into other text.

E.g. it can turn any character in the range [a-z] and make it uppercase [A-Z].

It takes input in the format

tr SET1 SET2

Set Examples

The sets can take multiple forms:

  • [abcd] [jhkl] = maps every character in SET1 to the corresponding SET2 one
  • [a-z] [A-Z] = see above


  • -c SET1 = complements the set, and hence removes everything in it.
    • -c [a-z], in this case, all lowercase characters are removed
  • -s SET1 = takes all duplicates of SET1, and replaces them with just 1.
    • -s ' ' replaces multiple spaces with just one
  • -d SET1 = deletes all the instances of the SET1 items
    • -d 'abc' = strips a's, b's and c's

It's useful for things like upper-to-lower casing.