Filtering with grep

Learn how to filter results with the grep command and regular expressions.

The grep command

When we’re dealing with program output, we often want to filter the results. The grep command lets us search the text for characters or phrases. We can use grep to search through program output or a file. Let’s explore grep by working with some files.

Execute the terminal below to create a file named words.txt that contains several words, each on its own line:

$ cat << 'EOF' > words.txt
> blue
> apple
> candy
> hand
> fork
> EOF

Now, we use grep to search the file for the word “and”:

$ grep 'and' words.txt

Run the complete code on the terminal below for practice.

cat << "EOF" > words.txt
blue
apple
candy
hand
fork
EOF
grep 'and' words.txt

Get hands-on with 1200+ tech skills courses.