Skip to main content

Searching and Finding Data in Files

Hands-On Lab

 

Photo of Michael Christian

Michael Christian

Course Development Director in Content

Length

00:45:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Searching and Finding Data in Files

Introduction

In this hands-on lab, we'll practice using grep, piping, and regular expressions to pull discrete data from a large file.

Connecting to the Lab

  1. Open your terminal application, and run the following command (remember to replace <PUBLIC_IP> with the public IP you were provided on the lab instructions page):
    ssh cloud_user@<PUBLIC_IP>

Determine How Many Words Start with the Letter "X"

  1. List the current working directory.
    pwd
  2. List the contents of the current working directory.
    ls
  3. List the number of lines in /usr/share/dict/words.
    cat /usr/share/dict/words | wc -l
  4. Determine how many words in the file start with the letter "x".
    cat /usr/share/dict/words | grep -E "^x" | wc -l
  5. Rerun the previous command, and send the output to the value.txt file.
    cat /usr/share/dict/words | grep -E "^x" | wc -l >> value.txt
  6. Verify that this was successful.
    cat 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. Rerun the previous command, and send the output to the value.txt file.
    cat /usr/share/dict/words | grep -E "x$" | wc -l >> value.txt
  3. Verify that this was successful.
    cat 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. Rerun the previous command, and send the output to the value.txt file.
    cat /usr/share/dict/words | grep -E "^l...x$" >> value.txt
  3. Verify that this was successful.
    cat 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. Rerun the previous command, and send the output to the value.txt file.
    cat /usr/share/dict/words | grep -E "^y..$|^z..$" | wc -l >> value.txt
  3. Verify that this was successful.
    cat value.txt

Conclusion

Congratulations, you've successfully completed this hands-on lab!