Test with Multiple Patterns
In this step, we will expand our knowledge of grep
to search for lines that match either of two patterns. This is useful when you want to find lines that contain one keyword or another.
We can use the -E
option with grep
to enable extended regular expressions, which allows us to use the |
symbol (the pipe symbol, but within the quotes) to mean "OR".
Let's try searching for lines that contain either "master" or "main" in the output of git branch
. Make sure you are still in the ~/project/my-time-machine
directory.
git branch | grep -E "master|main"
Since our repository currently only has the master
branch, the output will still be:
* master
However, if we had a branch named main
, this command would show both the master
and main
branches.
Let's simulate having another branch by creating one (we'll learn more about creating branches later):
git branch new-feature
Now, run the git branch
command again to see the new branch:
git branch
The output should show both branches:
* master
new-feature
Now, let's use grep -E
to search for lines containing "master" or "new-feature":
git branch | grep -E "master|new-feature"
The output should now show both lines:
* master
new-feature
This demonstrates how grep -E
with the |
operator can be used to filter output based on multiple patterns. This technique is very powerful when you need to find specific information in command-line output that might match one of several possibilities.