0
0
Bash Scriptingscripting~5 mins

grep in scripts in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the grep command do in a script?

grep searches for lines in a file or input that match a given pattern. It helps find specific text quickly.

Click to reveal answer
beginner
How do you use grep to search for the word "apple" in a file named fruits.txt?

Use the command: grep 'apple' fruits.txt. It will show all lines containing "apple".

Click to reveal answer
beginner
What does the -i option do in grep?

The -i option makes the search case-insensitive. It finds matches regardless of uppercase or lowercase letters.

Click to reveal answer
intermediate
How can grep be used in a script to check if a pattern exists and then run a command?

You can use grep with an if statement. For example:<br>if grep -q 'pattern' file.txt; then echo 'Found'; fi<br>This runs the echo only if the pattern is found.

Click to reveal answer
intermediate
What does the -q option do in grep?

The -q option makes grep run quietly. It does not print output but sets the exit status to indicate if a match was found.

Click to reveal answer
What will grep 'dog' pets.txt do?
AShow lines containing 'dog' in pets.txt
BDelete lines containing 'dog' in pets.txt
CReplace 'dog' with 'cat' in pets.txt
DCount the number of lines in pets.txt
Which grep option makes the search ignore case?
A-v
B-i
C-q
D-r
What does the -q option do in grep?
APrint matching lines
BSearch recursively
CRun quietly without output
DInvert match
How can you use grep in a script to run a command only if a pattern is found?
AUse <code>grep -v</code>
BUse <code>grep -i</code>
CUse <code>grep -r</code>
DUse <code>if grep -q 'pattern' file; then ... fi</code>
What will grep -v 'cat' animals.txt do?
AShow lines not containing 'cat'
BReplace 'cat' with another word
CCount lines with 'cat'
DShow lines containing 'cat'
Explain how you would use grep in a bash script to check if a file contains a specific word and then print a message.
Think about using <code>grep -q</code> inside an <code>if</code> condition.
You got /6 concepts.
    Describe the difference between using grep with and without the -i option.
    Consider how 'Apple' and 'apple' are treated.
    You got /3 concepts.