Grep Exercises

Questions and Answers

What is the purpose of Grep?

Click Here for Answer

Parse text and return lines that match specific patterns.

What are some common uses of Grep?

Click Here for Answer
  • Search for file names matching a specific pattern in a directory
  • Search contents of a file for specific patterns
  • Search web request data for lines matching specific patterns

What character represents the Line Begin Anchor?

Click Here for Answer

^

What character represents the Line End Anchor??

Click Here for Answer

$

What is the Any Character Symbol??

Click Here for Answer

.

How would you search a file for lines beginning with the phrase John?

Click Here for Answer
cat file-name | grep "^John"

How would you search a directory for only files ending in .csv?

Click Here for Answer
ls /path/to/directory | grep ".csv$"

Working with the user.csv Dataset

Using the user.csv Dataset complete the following requirements:

Note

If you need to get the user.csv Dataset again you can do so with the following command:

curl -s https://launchcodelearning.org/api/walkthrough/user?data_format=csv > user.csv

Filter the results of the user.csv so that you only match users by the name of James with an email ending in .org that work for Boeing.

Click Here for Solution
grep '^James' user.csv | grep '.org' | grep 'Boeing$'

Filter the results of the user.csv so that you only match users with the last name Campbell that work for the organization Freedom pay.

Click Here for Solution
grep "Campbell," user.csv | grep "Freedom pay"
Bonus

How would you filter the results of the user.csv so that you only match users with the first name John or Paul and have a last name beginning with the letter J or S that work for the organization Express Scripts?

Click Here for Answer
grep -E '^John,[J,S]|^Paul,[J,S]' user.csv | grep "Express Scripts"