Searching and Finding Data in Files

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

In this hands-on lab, we’ll practice using `grep`, piping, and regular expressions to pull discrete data from a large file. Being able to parse a large file and narrow down the output to just what you’re looking for is one of the strengths of the command line. We’re often called to work with large log files, and pull just the relevant bits for analysis. By the end of this lab, you will be comfortable using basic regular expressions to narrow the contents of a large file.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Determine How Many Words Start with the Letter “x”
  1. Run the following command:
    cat /usr/share/dict/words | grep -E "^x" | wc -l
  2. Send the output to ~/value.txt.
    cat /usr/share/dict/words | grep -E "^x" | wc -l >> ~/value.txt
Determine How Many Words End with the Letter “x”
  1. Run the following command:
    cat /usr/share/dict/words | grep -E "x$" | wc -l
  2. Send the output to ~/value.txt.
    cat /usr/share/dict/words | grep -E "x$" | wc -l >> ~/value.txt
Determine Which Word Starts with “l”, Ends in “x”, and Contains 5 Characters Total
  1. Run the following command:
    cat /usr/share/dict/words | grep -E "^l...x$"
  2. Send the output to ~/value.txt.
    cat /usr/share/dict/words | grep -E "^l...x$" >> ~/value.txt
Determine How Many 3-Letter Words Start with the Letters “y” or “z”
  1. Run the following command:
    cat /usr/share/dict/words | grep -E "^y..$|^z..$" | wc -l
  2. Send the output to ~/value.txt.
    cat /usr/share/dict/words | grep -E "^y..$|^z..$" | wc -l >> ~/value.txt

Additional Resources

Use your knowledge of piping and regular expressions to answer the following questions about the /usr/share/dict/words file, and send the output to ~/value.txt:

  • How many words start with the letter "x"?

  • How many words end with the letter "x"?

  • What word in this file starts with "l", ends in "x", and contains a total of 5 characters?

  • How many 3-letter words start with the letters "y" or "z"?

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?