Operating Systems & Administration

[Day 4] 4. Grep (GNU Regular Expression Parser)

D
DITISS Admin
••3 min read

Regular Expression / Pattern

  • Definition: A collection/set/combination of characters used for searching or matching purposes.

  • Types of Characters:

    1. Simple Characters: A-Z, a-z, 0-9
    2. Special/Wildcard/Meta Characters:
      .
      ,
      *
      ,
      +
      ,
      ?
      ,
      ()
      ,
      []
      ,
      {}
      ,
      \
      ,
      |
      , etc.
      • Basic Wildcard: Matches a single character.
      • Extended Wildcard: Offers additional matching capabilities.
  • Examples of Regular Expressions:

    • a
    • a+b
    • a(123)c
    • ai{2}c

Syntax for Using
grep
:

  • Basic syntax:
    grep "pattern" <filename>
    or
    grep [options] "pattern" <filename>

!4. Grep1.png

Common
grep
Options:

  • -w
    : Matches the exact pattern only.
  • -n
    : Prints matching lines with line numbers.
  • -v
    : Prints lines that do not contain the specified pattern.
  • -i
    : Ignores case sensitivity.
  • ^
    : Matches lines starting with the specified pattern. (e.g.,
    ^is
    )
  • $
    : Matches lines ending with the specified pattern. (e.g.,
    is$
    )
  • ^is$
    : Matches lines containing only this pattern.

!4. Grep2.png

Examples of Using
grep
:

  • grep b[aou]g select.txt
    : Matches single occurrences of
    b
    followed by any one character (a, o, or u) and then
    g
    .
  • grep b[^aou]g select.txt
    : Prints lines that do not have the specified pattern after the
    ^
    .
  • grep b.g select.txt
    : Prints lines with a single character between
    b
    and
    g
    .

!4. Grep3.png

  • Escape Sequences:
    • grep "b*g" select.txt
      : Asterisk
      *
      stands for 0 or more occurrences.
    • grep "b\*g" select.txt
      : Uses escape sequence to treat
      *
      as a regular character.
    • grep "b[*]g" select.txt
      : Treats
      *
      as a regular character.
    • grep -F "b*g" select.txt
      : Treats
      *
      as a literal character due to
      -F
      .
    • fgrep "b*g" select.txt
      : Similar function as above.

!4. Grep4.png

  • grep "ba?g" repeat.txt
    : The
    ?
    wildcard matches 0 or 1 occurrence of the preceding character.
  • grep -E "ba?g" repeat.txt
    : Uses extended wildcards, requiring
    -E
    .
  • egrep "ba?g" repeat.txt
    : Same function as above.
  • +
    : Matches 1 or more occurrences of the previous characters.

!4. Grep5.png

  • Exact Occurrences: Use
    {}
    for exact occurrences of a character.
  • Groups and Alternatives:
    • []
      : Matches a single occurrence of any character within brackets.
    • ()
      : Groups characters for combined matching.
    • |
      : Functions as an OR operator.

!4. Grep6.png !4. Grep7.png!4. Grep8.png

Data Validation

  • The
    grep
    command is commonly used for data validation purposes.

Read Next

8/26/2026

[Day 4] 5. Steps for VIM

1. **Open the Terminal.** 2. **Create and Open a File:** - Run the following command: `vim <filename>` - This will creat...

8/26/2026

[Day 5] 1. Commands

#### 1. `uname` Command - **Purpose**: Displays information about the system. - **Usage**: - `uname`: Shows the name of the opera...