Linux/Unix Tutorial
Estimated reading: 4 minutes 23 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

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

Or Copy Link

CONTENTS
Scroll to Top