Combining Pipes
Now let's build some real-world pipelines. These patterns come up constantly in DevOps and development.
The Top-N Pattern
Find the most common items:
Breaking it down:
cut -d' ' -f1- extract first field (IP address)sort- sort IPs (needed for uniq)uniq -c- count occurrencessort -rn- sort by count, highest firsthead -10- top 10
This Pattern is Gold
sort | uniq -c | sort -rn | head is one of the most useful patterns in Linux. Memorize it.
Log Analysis Pipelines
Errors Per Hour
Most Common Error Types
Process Analysis
Top CPU Users
Memory by User
File System Analysis
Largest Files
File Types Count
Network Analysis
Active Connections by Port
Connections Per IP
Text Processing Pipelines
Extract and Transform
Format Conversion
Building Incrementally
When building complex pipelines, build step by step:
# Start simple, verify each step
cat access.log | head
# Add filter
cat access.log | grep '500' | head
# Add extraction
cat access.log | grep '500' | cut -d' ' -f1 | head
# Add counting
cat access.log | grep '500' | cut -d' ' -f1 | sort | uniq -c
# Add sorting
cat access.log | grep '500' | cut -d' ' -f1 | sort | uniq -c | sort -rn
# Finalize
cat access.log | grep '500' | cut -d' ' -f1 | sort | uniq -c | sort -rn | head -10
Debug with head
Add | head at any point to see intermediate output without overwhelming your terminal.
xargs - Bridging Pipes
When commands don't read from stdin:
xargs converts stdin lines into arguments.
What does `sort | uniq -c | sort -rn | head` do?
Key Takeaways
- Build pipelines incrementally, testing each step
sort | uniq -c | sort -rn | head= top N pattern- Use
headto debug intermediate steps xargsbridges pipes to commands expecting arguments- Complex analysis is just simple commands combined
Next: the tee command for splitting output.