What other grep options are there?

QuestionsQuestions8 SkillsProUser Account ManagementOct, 29 2025
093

Here are some commonly used grep options that enhance its functionality:

  1. -i: Perform case-insensitive searching.

    grep -i "pattern" file.txt
  2. -c: Count the number of matching lines.

    grep -c "pattern" file.txt
  3. -n: Display line numbers along with matching lines.

    grep -n "pattern" file.txt
  4. -v: Invert the match, showing lines that do not match the pattern.

    grep -v "pattern" file.txt
  5. -r or -R: Recursively search through directories.

    grep -r "pattern" directory/
  6. -l: List only the names of files with matching lines.

    grep -l "pattern" *.txt
  7. -e: Specify multiple patterns to search for.

    grep -e "pattern1" -e "pattern2" file.txt
  8. -A: Show lines after the match (e.g., -A 3 shows 3 lines after).

    grep -A 3 "pattern" file.txt
  9. -B: Show lines before the match (e.g., -B 2 shows 2 lines before).

    grep -B 2 "pattern" file.txt
  10. -C: Show lines around the match (e.g., -C 2 shows 2 lines before and after).

    grep -C 2 "pattern" file.txt

These options can be combined to refine your search. For example:

grep -i -n "pattern" file.txt

Feel free to ask if you need more information on any specific option!

0 Comments

no data
Be the first to share your comment!