Skip to main content
Engineering LibreTexts

05-D.7.1: Handling Text Files - tr/wc Command

  • Page ID
  • The tr Command

    The tr command is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. tr stands for translate.


    tr [ OPTION ] SET1 [SET2]

    Command Options:

    Options Option Meaning
    -c Complements the set of characters in string.i.e., operations apply to characters not in the given set.
    -d Delete characters in the first set from the output.
    -s Replaces repeated characters listed in the set1 with single occurrence.
    -t Truncates set1.

    The tr command is pretty powerful, but also quite simple. If you get good at working with regular expressions you can do some complex things with tr, but mostly it is used with simple types of translations.

    The example below shows the content of the file junk.txt, just two lines of lowercase text:

    The tr command can translate characters between upper and lower case, and deleting characters.
    Figure \(\PageIndex{1}\): The cat Command. ("cat command" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    The first tr command gives the range [a-z], which says to take any character between a and z. The second range is [A-Z]. So the tr command says take the characters that match the first argument and translate them to the appropriate character in the second argument.

    The second tr invocation uses the :lower: and :upper: arguments to accomplish the same results.

    The wc Command

    The wc command is mainly used for counting purpose. wc stands for word count. It is used to find out the number of lines, word count, byte and characters count in the files specified in the file arguments. By default it displays four-columnar output. The first column shows the number of lines present in a file specified, second column shows number of words present in the file, third column shows number of characters present in a file and fourth column itself is the file name which is given as argument.


    wc [OPTION]... [FILE]...

    Command Options:

    Options Option Meaning
    -c, --bytes Print the byte counts.
    -m, --chars Print the character counts.
    -l, --lines Print the newline counts.
    -w, --words Print the word counts.

    The wc command is simple - it provides a variety of counts: lines, words and characters. The default is that wc display all three of the outputs, and using the options allows the user to choose which output is desired.

    The wc command can not only count the number of words in a file, with the -l option the output will be the number of lines in a file.
    Figure \(\PageIndex{1}\): The wc Command. ("wc command" by Patrick McClanahan is licensed under CC BY-SA 4.0)

    Adapted from:
    "tr command in Unix/Linux with examples" by Shivani Ghughtyal, Geeks for Geeks is licensed under CC BY-SA 4.0
    "wc command in Linux with examples" by Akash Gupta, Geeks for Geeks is licensed under CC BY-SA 4.0

    • Was this article helpful?