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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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?