Code-Memo

Grep

grep is a powerful command-line utility in Linux used for searching plain-text data sets for lines that match a regular expression, and its name stands for “global regular expression print.”

Basic Syntax

grep [OPTIONS] PATTERN [FILE...]

Commonly Used Options

Examples

  1. Basic Search
    grep "hello" file.txt
    

    Searches for the string “hello” in file.txt.

  2. Case-Insensitive Search
    grep -i "hello" file.txt
    

    Searches for “hello” in a case-insensitive manner in file.txt.

  3. Search Recursively
    grep -r "hello" /path/to/directory
    

    Searches for “hello” in all files under /path/to/directory.

  4. Count Matching Lines
    grep -c "hello" file.txt
    

    Counts the number of lines that contain “hello” in file.txt.

  5. Display Line Numbers
    grep -n "hello" file.txt
    

    Displays matching lines along with their line numbers in file.txt.

  6. Search for Whole Words
    grep -w "hello" file.txt
    

    Searches for lines containing the whole word “hello” in file.txt.

  7. Invert Match
    grep -v "hello" file.txt
    

    Displays lines that do not contain “hello” in file.txt.

  8. Multiple Files
    grep "hello" file1.txt file2.txt
    

    Searches for “hello” in both file1.txt and file2.txt.

Using Regular Expressions

grep supports extended regular expressions (ERE) with the -E option, which allows for more complex pattern matching.

grep -E "hello|world" file.txt

This searches for lines containing either “hello” or “world” in file.txt.

Piping and Redirection

grep is often used in combination with other commands using pipes.

cat file.txt | grep "hello"

This searches for “hello” in the output of cat file.txt.

Grep in Scripts

grep is frequently used in shell scripts for processing and filtering text data. Here’s a simple example:

#!/bin/bash
# Script to find and count occurrences of "error" in log files
for file in /var/log/*.log; do
    echo "Processing $file"
    grep -c "error" "$file"
done