Operating Systems & Administration
[Day 4] 4. Grep (GNU Regular Expression Parser)
D
DITISS AdminRegular Expression / Pattern
-
Definition: A collection/set/combination of characters used for searching or matching purposes.
-
Types of Characters:
- Simple Characters: A-Z, a-z, 0-9
- Special/Wildcard/Meta Characters: ,
.,*,+,?,(),[],{},\, etc.|- Basic Wildcard: Matches a single character.
- Extended Wildcard: Offers additional matching capabilities.
-
Examples of Regular Expressions:
aa+ba(123)cai{2}c
Syntax for Using grep
:
grep- Basic syntax:
or
grep "pattern" <filename>grep [options] "pattern" <filename>
!4. Grep1.png
Common grep
Options:
grep- : Matches the exact pattern only.
-w - : Prints matching lines with line numbers.
-n - : Prints lines that do not contain the specified pattern.
-v - : Ignores case sensitivity.
-i - : Matches lines starting with the specified pattern. (e.g.,
^)^is - : Matches lines ending with the specified pattern. (e.g.,
$)is$ - : Matches lines containing only this pattern.
^is$
!4. Grep2.png
Examples of Using grep
:
grep- : Matches single occurrences of
grep b[aou]g select.txtfollowed by any one character (a, o, or u) and thenb.g - : Prints lines that do not have the specified pattern after the
grep b[^aou]g select.txt.^ - : Prints lines with a single character between
grep b.g select.txtandb.g
!4. Grep3.png
- Escape Sequences:
- : Asterisk
grep "b*g" select.txtstands for 0 or more occurrences.* - : Uses escape sequence to treat
grep "b\*g" select.txtas a regular character.* - : Treats
grep "b[*]g" select.txtas a regular character.* - : Treats
grep -F "b*g" select.txtas a literal character due to*.-F - : Similar function as above.
fgrep "b*g" select.txt
!4. Grep4.png
- : The
grep "ba?g" repeat.txtwildcard matches 0 or 1 occurrence of the preceding character.? - : Uses extended wildcards, requiring
grep -E "ba?g" repeat.txt.-E - : Same function as above.
egrep "ba?g" repeat.txt - : 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 command is commonly used for data validation purposes.
grep
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...