Skip to main content
Engineering LibreTexts

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

  • Page ID
    32213
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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.

    Syntax:

    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 classes.txt, just two lines of lowercase text:

    pbmac@ubuntu $ cat classes.txt 
    Linux
    Operating Systems
    Introduction to Cybersecurity
    pbmac@ubuntu $ cat classes.txt | tr "[a-z]" "[A-Z]"
    LINUX
    OPERATING SYSTEMS
    INTRODUCTION TO CYBERSECURITY
    pbmac@ubuntu $ cat classes.txt | tr "[::lower:]" "[:upper:]"
    tr: invalid character class ‘:lower’
    pbmac@ubuntu $ cat classes.txt | tr "[:lower:]" "[:upper:]"
    LINUX
    OPERATING SYSTEMS
    INTRODUCTION TO CYBERSECURITY
    pbmac@ubuntu $ 
    

     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.

    Syntax:

    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.

    pbmac@ubuntu $ wc classes.txt nextsemester.txt 
     3  6 54 classes.txt
     3  3 27 nextsemester.txt
     6  9 81 total
    pbmac@ubuntu $ wc -l classes.txt nextsemester.txt 
     3 classes.txt
     3 nextsemester.txt
     6 total
    pbmac@ubuntu $ wc -c classes.txt nextsemester.txt 
    54 classes.txt
    27 nextsemester.txt
    81 total
    pbmac@ubuntu $ 
    

     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


    05-D.7.1: Handling Text Files - tr/wc Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?