grep Flashcards
What does the -i option do in grep?
The -i option makes the search case-insensitive. For example, ‘grep -i error file.txt’ matches ‘error’, ‘Error’, and ‘ERROR’.
What does the -v option do in grep?
The -v option inverts the match, displaying lines that do NOT match the pattern. For example, ‘grep -v DEBUG file.txt’ excludes lines containing ‘DEBUG’.
What does the -c option do in grep?
The -c option counts the number of matching lines instead of displaying them. For example, ‘grep -c keyword file.txt’ outputs the number of lines containing ‘keyword’.
What does the -n option do in grep?
The -n option displays line numbers alongside matching lines. For example, ‘grep -n "pattern" file.txt’ shows matching lines with their line numbers.
What does the -l option do in grep?
The -l option lists only the names of files containing matches. For example, ‘grep -l "error" *.log’ lists log files containing ‘error’.
What does the -L option do in grep?
The -L option lists only the names of files that do NOT contain any matches. For example, ‘grep -L "keyword" *.txt’ lists files that do not contain ‘keyword’.
What does the -r option do in grep?
The -r option enables recursive search through directories. For example, ‘grep -r "keyword" /project’ searches all files in the /project directory and subdirectories.
What does the -w option do in grep?
The -w option matches whole words only. For example, ‘grep -w "cat" file.txt’ matches ‘cat’ as a whole word but not ‘category’.
What does the -A option do in grep?
The -A option displays ‘n’ lines after each match. For example, ‘grep -A 3 "error" file.log’ shows the matching lines and 3 lines after each match.
What does the -B option do in grep?
The -B option displays ‘n’ lines before each match. For example, ‘grep -B 2 "404" file.log’ shows 2 lines before each matching line.
What does the -C option do in grep?
The -C option displays ‘n’ lines of context before and after each match. For example, ‘grep -C 1 "failed" file.log’ shows 1 line before and 1 line after each match.
What does the -e option do in grep?
The -e option allows specifying multiple patterns. For example, ‘grep -e "pattern1" -e "pattern2" file.txt’ matches lines containing ‘pattern1’ or ‘pattern2’.
What does the -f option do in grep?
The -f option allows reading patterns from a file. For example, ‘grep -f patterns.txt file1.txt’ matches lines containing patterns listed in patterns.txt.
What does the –color option do in grep?
The –color option highlights matching text in the output. For example, ‘grep –color "keyword" file.txt’ highlights ‘keyword’ in the results.
What does the -q option do in grep?
The -q option runs grep quietly, suppressing all output. For example, ‘grep -q "ERROR" file.txt && echo "Error found"’ outputs ‘Error found’ if ‘ERROR’ exists.
What does the grep command ‘grep -i "error" /var/log/syslog’ do?
It performs a case-insensitive search for the word ‘error’ in the /var/log/syslog file and displays all matching lines.
What does the grep command ‘grep -v "debug" /var/log/messages’ do?
It displays all lines from /var/log/messages that do not contain the word ‘debug’.
What does the grep command ‘grep -c "timeout" application.log’ do?
It counts the number of lines in application.log that contain the word ‘timeout’.
What does the grep command ‘grep -n "keyword" notes.txt’ do?
It searches for the word ‘keyword’ in notes.txt and displays the matching lines along with their line numbers.
What does the grep command ‘grep -l "failure" /var/log/*.log’ do?
It lists the names of log files in /var/log that contain the word ‘failure’.
What does the grep command ‘grep -L "success" results.txt’ do?
It lists the names of files that do not contain the word ‘success’ in results.txt.
What does the grep command ‘grep -r -i "critical" /etc’ do?
It recursively searches for the word ‘critical’ in all files under the /etc directory, case-insensitively.
What does the grep command ‘grep -w "error" /var/log/syslog’ do?
It searches for whole-word occurrences of ‘error’ in /var/log/syslog and excludes matches where ‘error’ is part of another word.
What does the grep command ‘grep -A 2 "failed" logs.txt’ do?
It searches for lines containing ‘failed’ in logs.txt and displays each matching line along with the 2 lines after it.