--- Learn GREP ---

Essential Grep Commands

Here are 25 examples of the grep command, each with a brief explanation and an example of the output.
The examples use a sample file named example.txt which contains the following text:

Hello World
This is a test file.
GREP is a powerful tool.
line 1: cat dog bird
Line 2: fish mouse
This line contains the word test.
12345
testing is fun
This is another line.
  1. Basic Search

    To find lines containing the pattern test.

    
    $ grep "test" example.txt
    This is a test file.
    This line contains the word test.
    
  2. Case-Insensitive Search

    Use the -i option to ignore case, finding lines with "Test," "TEST," or "tEsT."

    
    $ grep -i "test" example.txt
    This is a test file.
    This line contains the word test.
    testing is fun
    
  3. Invert Match Use the -v option to display lines that do not contain the pattern.
    
    $ grep -v "test" example.txt
    Hello World
    GREP is a powerful tool.
    line 1: cat dog bird
    Line 2: fish mouse
    12345
    This is another line.
    
  4. Count Matches The -c option counts the number of lines that match the pattern.
    
    $ grep -c "line" example.txt
    2
    
    Use the -n option to show the line number for each match.
    
    $ grep -n "line" example.txt
    4:line 1: cat dog bird
    6:This line contains the word test.
    9:This is another line.
    
  5. Whole Word Search The -w option ensures only a whole word is matched, not a substring.
    
    $ grep -w "test" example.txt
    This is a test file.
    This line contains the word test.
    
  6. Exact Line Match Using the -x option, grep will only match lines that are exactly the same as the pattern.
    
    $ grep -x "12345" example.txt
    12345
    
  7. Recursive Search The -r option searches for a pattern in all files within a directory and its subdirectories.
    
    $ grep -r "Hello" .
    ./example.txt:Hello World
    
  8. List Matching Files The -l option lists only the names of the files that contain the pattern.
    
    $ grep -l "test" *.txt
    example.txt
    
  9. List Non-Matching Files The -L option lists the names of files that do not contain the pattern.
    
    $ grep -L "Hello" *.txt
    another_file.txt
    
  10. Search Multiple Patterns Use the -e option for multiple patterns or use the | (OR) operator with the -E option.
    
    $ grep -E "dog|mouse" example.txt
    line 1: cat dog bird
    Line 2: fish mouse
    
  11. Search from Standard Input grep can read from a pipe. Here, the output of cat is piped to grep.
    
    $ cat example.txt | grep "World"
    Hello World
    
  12. Show Context Before a Match Use the -B option to show a specified number of lines before the match.
    
    $ grep -B 1 "bird" example.txt
    GREP is a powerful tool.
    line 1: cat dog bird
    
  13. Show Context After a Match Use the -A option to show a specified number of lines after the match.
    
    $ grep -A 1 "dog" example.txt
    line 1: cat dog bird
    Line 2: fish mouse
    
  14. Show Context Around a Match The -C option shows a specified number of lines of context around the match.
    
    $ grep -C 1 "dog" example.txt
    GREP is a powerful tool.
    line 1: cat dog bird
    Line 2: fish mouse
    
  15. Quiet Mode The -q option runs in quiet mode, suppressing output and only returning an exit status (0 for success, 1 for failure).
    
    $ grep -q "World" example.txt && echo "Pattern found" || echo "Pattern not found"
    Pattern found
    
  16. Fixed Strings The -F option interprets the pattern as a fixed string, disabling regular expression features.
    
    $ grep -F "GREP" example.txt
    GREP is a powerful tool.
    
  17. Search for Blank Lines Using the regular expression ^$ will find all blank lines.
    
    $ grep "^$" example.txt
    (empty output because example.txt has no blank lines) 
  18. Search for Lines Starting with a Pattern Use the ^ (caret -- shift 6) anchor to match a pattern at the beginning of a line.
    
    $ grep "^This" example.txt
    This is a test file.
    This line contains the word test.
    This is another line.
    
  19. Search for Lines Ending with a Pattern Use the $ (dollar sign) anchor to match a pattern at the end of a line.
    
    $ grep "World$" example.txt
    Hello World
    
  20. Using a Pattern from a File The -f option reads patterns from a file, one pattern per line.
    
    $ cat patterns.txt
    test
    fish
    $ grep -f patterns.txt example.txt
    This is a test file.
    Line 2: fish mouse
    This line contains the word test.
    testing is fun
    
  21. Display Only the Match The -o option displays only the matched part of a line, with each match on a new line.
    
    $ grep -o "is" example.txt
    is
    is
    is
    is
    
  22. Search Using a Regular Expression Using grep's default regular expression support, this matches lines containing a number.
    
    $ grep "[0-9]" example.txt
    line 1: cat dog bird
    Line 2: fish mouse
    12345
    
  23. Search with Multiple Files grep can search through multiple files at once.
    
    $ grep "Hello" file1.txt file2.txt
    file1.txt:Hello World
    
  24. Search for a Special Character To search for a special character like a dollar sign $ you must escape it with a backslash \.
    
    $ grep "\$" example.txt
     (empty output because example.txt has no dollar signs) 
 
grep -oh '[^"]*\.html' filename

Or if you want to be more specific and match quoted strings:
bash

grep -oh '"[^"]*\.html"' filename

Explanation:

    -o = only output the matching part (not the whole line)
    -h = suppress filename in output (useful with multiple files)
    [^"]*\.html = matches any characters except quotes, followed by .html
    The \. escapes the dot so it matches a literal period

If you want to remove the quotes from the output, you can pipe it through sed:
bash

grep -oh '"[^"]*\.html"' filename | sed 's/"//g'

Try the first one - it should grab all your .html filenames!


Valid HTML 4.0 Transitional