Linux/Unix Tutorial
Estimated reading: 4 minutes 277 views

Linux/Unix: Searching, Text Processing & Regex – grep, find, sed, awk, and Pattern Matching

Introduction – Supercharge Your Shell with Search & Pattern Tools

In Linux/Unix, the real power of the command line lies in text processing and file searching. Whether you’re filtering logs, extracting data, or matching patterns with regular expressions, tools like grep, find, awk, and sed turn simple commands into data-processing powerhouses.

In this guide, you’ll learn:

  • How to search inside files with grep, fgrep, and egrep
  • How to locate files with find, locate, which, and whereis
  • How to manipulate structured data using awk and sed
  • How to write and apply regular expressions for powerful matching

Topics Covered

Subtopic Description
Text Search (grep, fgrep, egrep)Search file content for strings or patterns
File Location (find, locate, which)Find files by name, path, or type
Text Processing (awk, sed)Extract and modify fields, lines, or characters in files
Regex ConceptsLearn how regular expressions work and how to use them effectively

Linux/Unix: Text Search (grep, fgrep, egrep)

grep – Search Lines for a Pattern

grep "error" logfile.txt

Displays lines containing β€œerror”.


-i, -n, and -r Options

grep -i "warning" logfile.txt   # Case-insensitive
grep -n "fail" logfile.txt      # Show line numbers
grep -r "config" /etc           # Recursive directory search

fgrep – Literal Search (No Regex)

fgrep "a.b" file.txt

Searches for exact string a.b, not regex.


egrep – Extended Regex Support

egrep "error|fail" logs.txt

Matches either β€œerror” or β€œfail”.


Linux/Unix: File Location (find, locate, which, whereis)

find – Search Files by Name, Type, Time

find /home -name "*.txt"

Finds all .txt files recursively.

find /etc -type f -size +1M

Finds files over 1MB.


locate – Fast Filename Search (uses database)

locate passwd

Run updatedb to refresh the database.


which & whereis – Command Location

which python3     # Shows full path
whereis python3   # Shows binary, source, man

Linux/Unix: Text Processing (awk, sed)

awk – Process Fields in Files

awk '{print $1, $3}' data.txt

Prints the first and third columns.

awk -F: '{print $1}' /etc/passwd

Uses : as a field separator.


sed – Stream Editor for Replacements

sed 's/error/ERROR/g' logfile.txt

Replaces β€œerror” with β€œERROR” in every line.

sed -n '2,4p' file.txt

Prints only lines 2 through 4.


Linux/Unix: Regex Concepts & Practice

Basic Metacharacters

PatternMeaning
.Any single character
*Zero or more of previous
^Start of line
$End of line
[]Match character set
``

Regex Example with grep -E

grep -E "^[A-Z].*\.log$" files.txt

Matches lines that start with a capital letter and end in .log.


Practice Regex with grep or sed

grep -E "user[0-9]{2}" names.txt
sed -E 's/[0-9]+/NUM/' numbers.txt

Summary – Recap & Next Steps

The tools in this guide are the foundation of shell data manipulation. When combined, they allow for automated parsing, dynamic reporting, and intelligent searchingβ€”right from the terminal.

Key Takeaways:

  • Use grep, fgrep, and egrep for file content filtering
  • Use find, locate, which to locate files or commands
  • Use awk and sed to process fields, rows, and text replacements
  • Regular expressions let you match patterns and automate logic

Real-World Applications:

  • Log parsing for DevOps and monitoring
  • Data extraction and reporting from structured files
  • Finding configuration files or scripts across directories

Frequently Asked Questions

What’s the difference between grep, fgrep, and egrep?
grep uses basic regex, fgrep searches fixed strings (no regex), and egrep allows extended regex.


How do I search all files for a keyword recursively?
Use:

grep -r "keyword" .

Can I use find to delete files?
Yes:

find . -name "*.log" -delete

How do I replace text in-place using sed?
Use:

sed -i 's/foo/bar/g' file.txt

What’s the best way to count unique values in a column?
Combine awk, sort, and uniq:

awk '{print $2}' data.txt | sort | uniq -c

Share Now :
Share

πŸ” Linux/Unix: Searching, Text Processing & Regex

Or Copy Link

CONTENTS
Scroll to Top