Part 2: Reading & Searching - Master Text Processing
Table of Contents
Learn to view, search, and process text files using the Linux command line In Part 1, you learned to navigate and create files. Now let’s learn to read them and search through them like a pro. The simplest way to view a file. Dumps the entire content to your screen. Exercise 2.1: When to use: Small files (< 100 lines). For large files, use A “pager” that lets you scroll through files without loading everything into memory. Navigation inside Exercise 2.2: View the beginning of a file. Exercise 2.3: View the end of a file. Super useful for log files! Exercise 2.4: Real-world use case: Count lines, words, and characters. Exercise 2.5: The most powerful search tool. Find lines matching a pattern. Basic usage: Important flags: Exercise 2.6: Real-world use case: Search for files by name, type, size, etc. Exercise 2.7: Combining find + grep: Pipes connect commands. The output of one becomes the input of the next. Example workflows: Exercise 2.8: Exercise 2.9: Exercise 2.10: Note: File must be sorted first! Exercise 2.11: Exercise 2.12: Exercise 2.13: Ready to practice? Complete the hands-on challenge in the workshop repository: Challenge: Analyze real web server logs to extract insights using grep, pipes, and text processing tools. The challenge includes: Clone the repository and give it a try:Navigation
Viewing File Contents
cat - Concatenate and Display
less.less - View Large Files
less:SPACE or f - Next pageb - Previous page/searchterm - Search forward?searchterm - Search backwardn - Next search resultN - Previous search resultg - Go to startG - Go to endq - Quit# Create a large file
# Try: Search for "500" by typing /500
# Press 'q' to quit
head - First Lines
tail - Last Lines
tail -f /var/log/nginx/access.log to watch web server requests in real-time.wc - Word Count
|
Searching Through Text
grep - Global Regular Expression Print
# Create a sample log file
# Now search:
grep -r "api_key" . to find hardcoded secrets in your codebase.find - Find Files
# Create some test files
# Now find:
# Translation: Find all .py files and search for "TODO" in them
The Power of Pipes (
|) | |
# Count how many .txt files exist
|
# Find the 5 largest files
| |
# Search logs and count errors
| |
# Get unique error messages
| | |
# How many lines in numbers.txt contain "5"?
|
# Show me just the ERROR lines from app.log, sorted
|
# Find all .txt files and show their line counts
Redirection
Output Redirection
# Create a file
# Append to it
# Redirect command output
Input Redirection
Advanced Text Processing
sort - Sort Lines
uniq - Remove Duplicates | | | |
|
cut - Extract Columns
tr - Translate Characters |
|
|
Hands-On Challenge
Quick Reference Card
# View Files
# Search
# Process Text
# Pipes & Redirection
|