USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Text Searching and Pattern Matching

Lesson 17/49 | Study Time: 20 Min

Text searching and pattern matching are fundamental operations in Linux, enabling users and administrators to efficiently locate, filter, and analyze data within files. Linux provides powerful and flexible tools to search for specific words, phrases, or complex patterns using regular expressions. 

The grep Command: Global Regular Expression Print

grep is the most widely used command for searching text that matches a specified pattern. It scans files line-by-line, printing matching lines to the output.


Basic Syntax:

text
grep [options] pattern [file...]


Example:

text
grep "error" /var/log/syslog

This command searches for the word "error" in the syslog file.


Common Options:


1. -i: Case-insensitive search.

2. -v: Invert match; display lines that do not contain the pattern.

3. -n: Show line numbers with matches.

4. -r or -R: Recursive search in directories.

5. -w: Match whole words only.

6. -c: Count the number of matching lines.

Regular Expressions (Regex)

Regular expressions allow defining complex search patterns.


Anchors:


  • ^: Start of line.
  • $: End of line.


Character classes:


  • [abc]: Matches any one character 'a', 'b', or 'c'.
  • [^abc]: Matches any character except 'a', 'b', or 'c'.


Quantifiers:


*: Zero or more occurrences.

+: One or more occurrences.

?: Zero or one occurrence.

{n,m}: Between n and m occurrences.


Special characters:


  • .: Matches any single character.
  • \: Escape special characters.

Extended and Perl-Compatible Regular Expressions


1. Use grep -E or the egrep command to enable extended regex for more expressive patterns.

2. Use grep -P to enable Perl-compatible regex (PCRE) for advanced features like lookaheads or lazy matching.

Searching Text in Multiple Files and Real-Time Monitoring

Linux command-line utilities make text searching fast and efficient. Following are techniques for searching multiple files and monitoring output in real time.


  • Search multiple files:
text
grep "TODO" *.txt


  • Recursive search in all files in a directory:
text
grep -r "TODO" /path/to/directory


  • Monitor log files in real-time:
text
tail -f /var/log/syslog | grep "error"

Practical Examples


1. Find lines beginning with "Start":

text
grep "^Start" logfile.txt


2. Find lines ending with "success":

text
grep "success$" logfile.txt


3. Find all lines that do not contain "failed":

text
grep -v "failed" logfile.txt


4. Count occurrences of "warning":

text
grep -c "warning" logfile.txt


5. Ignore case while searching for "User":

text
grep -i "user" logfile.txt
Samuel Wilson

Samuel Wilson

Product Designer
Profile

Class Sessions

1- What is Linux and Operating System Concepts 2- Linux History and Evolution 3- Linux Distributions and Their Purposes 4- Open Source Software and Licensing 5- Graphical User Interface (GUI) and Desktop Environments 6- Terminal Access and Command-Line Fundamentals 7- Getting Help and Command Documentation 8- File System Hierarchy and Directory Structure 9- Navigating Directories and Listing Contents 10- Creating, Copying, and Moving Files and Directories 11- Deleting Files and Directories 12- Symbolic and Hard Links 13- Understanding File Permissions Model 14- Modifying Permissions and Ownership 15- User and Group Management 16- Sudo and Privilege Escalation 17- Text Searching and Pattern Matching 18- Text Processing and Stream Editing 19- Compressing and Archiving Files 20- Text Editing and File Creation 21- Package Management Systems Overview 22- Installing and Updating Software with APT 23- Installing and Updating Software with YUM/DNF 24- Managing Software from Non-Repository Sources 25- Understanding Processes and Process Management 26- Viewing Running Processes 27- Process Control and Termination 28- Task Scheduling with Cron 29- Networking Concepts and IP Addressing 30- Viewing and Configuring Network Interfaces 31- Basic Network Troubleshooting 32- Shell Script Basics 33- Variables and Data Types 34- Conditional Logic in Scripts 35- Loops and Iteration 36- Functions and Code Reuse 37- Input/Output and User Interaction 38- System Authentication and Access Control 39- File System Security 40- Software Updates and Patching 41- Basic Firewall Concepts 42- System Information and Monitoring 43- Service and Daemon Management 44- System Boot Process and Runlevels 45- System Backup and Disaster Recovery 46- Comprehensive File System Management 47- System Automation Workflows 48- Multi-Concept Troubleshooting Scenarios 49- Continued Learning Pathways