0
0
Bash Scriptingscripting~5 mins

Basic regex in grep in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the command grep 'pattern' filename do?
It searches the file named filename for lines that match the given pattern and prints those lines.
Click to reveal answer
beginner
What is a regular expression (regex) in the context of grep?
A regex is a special text pattern that describes a set of strings. grep uses regex to find matching text in files.
Click to reveal answer
beginner
How do you use grep to search for lines that start with the word 'Hello'?
Use the caret symbol ^ to match the start of a line: grep '^Hello' filename.
Click to reveal answer
beginner
What does the dot . mean in a regex pattern?
The dot . matches any single character except a newline.
Click to reveal answer
intermediate
How can you search for lines containing either 'cat' or 'dog' using grep?
Use the alternation symbol \| inside quotes: grep 'cat\|dog' filename.
Click to reveal answer
Which symbol in regex matches the start of a line?
A*
B$
C.
D^
What does the regex pattern grep 'a.b' filename match?
ALines containing 'a' followed by any character, then 'b'
BLines containing 'ab' only
CLines starting with 'a' and ending with 'b'
DLines containing 'a' or 'b'
How do you tell grep to treat the pattern as a basic regex?
AUse <code>grep -E</code>
BUse <code>grep -P</code>
CUse <code>grep -G</code>
DUse <code>grep -F</code>
Which grep option allows using extended regex with alternation | without escaping?
A-E
B-F
C-P
D-G
What does the regex symbol $ match?
AStart of a line
BEnd of a line
CAny character
DZero or more repetitions
Explain how to use grep with a basic regex to find lines starting with a specific word.
Think about how to anchor your search to the line start.
You got /3 concepts.
    Describe the difference between basic and extended regex in grep and when to use each.
    Consider how you write alternation patterns.
    You got /3 concepts.