Grep Options Flashcards

(8 cards)

1
Q

What does grep -i do?

A

Performs case-insensitive search, ignoring uppercase/lowercase differences
Example: grep -i “error” logfile.txt matches “Error”, “ERROR”, “error”
Essential for flexible pattern matching in logs and text files

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of grep -r or grep -R?

A
  • Recursively searches through directories and subdirectories
  • -r follows symbolic links, -R does not follow symbolic links
  • Example: grep -r “function” /home/user/scripts/ searches all files in directory tree
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does grep -n help with search results?

A
  • Shows line numbers where matches are found
  • Makes it easier to locate and edit specific lines in files
  • Example: grep -n “TODO” *.py shows line numbers for each match
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does grep -v accomplish?

A
  • Inverts the search - shows lines that do NOT match the pattern
  • Useful for filtering out unwanted content or debugging
  • Example: grep -v “^#” config.file shows non-comment lines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When would you use grep -l vs grep -L?

A
  • -l lists only filenames that contain matches (not the matches themselves)
  • -L lists filenames that do NOT contain matches
  • Useful for finding which files contain or lack specific content
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is grep -c used for?

A
  • Counts the number of matching lines (not individual matches)
  • Returns numeric count instead of showing actual matches
  • Example: grep -c “error” /var/log/messages returns count of error lines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How does grep -w differ from regular grep?

A
  • Matches whole words only, not partial word matches
  • Prevents false positives when searching for short terms
  • Example: grep -w “cat” matches “cat” but not “catalog” or “concatenate”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does grep -A, grep -B, grep -C control?

A
  • -A n shows n lines After each match
  • -B n shows n lines Before each match
  • -C n shows n lines of Context (both before and after)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly