grep
is a command-line utility used for searching and filtering text files based on specified patterns or regular expressions. It displays the matching lines or filenames containing the pattern
Command | Description | Example | Output |
---|---|---|---|
grep pattern file | Search for a pattern in a single file. | grep "hello" file.txt | This is a sample file containing hello world. Another line with the word hello. |
grep pattern file1 file2 | Search for a pattern in multiple files. | grep "error" file1.txt file2.txt | file1.txt: An error occurred during the process. file2.txt: Error: Connection timeout. |
grep -r pattern directory | Recursively search for a pattern in a directory and its subdirectories. | grep -r "TODO" ./src | ./src/main.js: // TODO: Implement the feature. ./src/components/todo.jsx: <h1>TODO List</h1> |
grep -i pattern file | Perform a case-insensitive search. | grep -i "apple" fruits.txt | There’s an Apple tree in the garden. I like apples and oranges. |
grep -n pattern file | Display line numbers along with matching lines. | grep -n "function" script.js | 1: function greet() { 4: const sayHello = function() { |
grep -v pattern file | Invert the search and display non-matching lines. | grep -v "success" logs.txt | Error: Failed to load resource. Warning: Disk space low. Info: Application started. |
grep -l pattern file1 file2 | List filenames containing the pattern. | grep -l “error” *.log | logfile.txt |
grep -w pattern file | Match whole words only. | grep -w “cat” animals.txt | The cat chased a mouse. |
`grep -E “pattern1 | pattern2″ file` | grep -E “apple|orange” fruits.txt | There’s an Apple tree in the garden. I like apples and oranges. |
grep -A 2 pattern file | Display 2 lines after each match. | grep -A 2 “ERROR” logfile.txt | Line 72: ERROR – Null pointer exception. Line 73: Method call that caused the error. Line 74: Additional information about the error. |
grep -B 1 pattern file | Display 1 line before each match. | grep -B 1 “warning” log.txt | Line 28: WARNING – This action is irreversible. Line 29: Proceed with caution. |
grep -C 2 pattern file | Display 2 lines before and after each match. | grep -C 2 “important” notes.txt | Line 14: This is an important message. Line 15: Please take note of this information. Line 16: Also, remember the following details. |
grep -o pattern file | Show only the matched part of the line. | grep -o “user[0-9]+” data.txt | user1234 user9876 user2468 |
grep -q pattern file | Quiet mode: suppress output, set exit status. | grep -q "pattern" file && echo "Found" | Found |
File List
file.txt
This is a sample file containing hello world. Another line with the word hello. Some random text.
file1.txt
This is file1. An error occurred during the process. Some other text here.
file2.txt
This is file2. Error: Connection timeout. Additional lines in file2.
fruits.txt
There's an Apple tree in the garden. I like apples and oranges. I prefer bananas.
script.js
function greet() { console.log("Hello!"); } const sayHello = function() { console.log("Saying hello!"); }; // Some comments here.
log.txt
Error: Failed to load resource. Warning: Disk space low. Info: Application started.
notes.txt
Line 14: This is an important message. Line 15: Please take note of this information. Line 16: Also, remember the following details. Line 17: Another note.
data.txt
user1234 data1 user9876 data2 user2468 data3