Practical Redirection Methods
Stream Manipulation Techniques
1. Filtering and Processing Streams
graph LR
A[Input Stream] --> B{Stream Processing}
B --> C[grep]
B --> D[sed]
B --> E[awk]
Filtering with grep
## Capture lines containing specific patterns
command | grep "error"
## Case-insensitive search
command | grep -i "warning"
Text Processing with sed
## Replace text in stream
command | sed 's/old/new/g'
## Delete specific lines
command | sed '/pattern/d'
Advanced Redirection Scenarios
Handling Large Output Streams
Technique |
Command |
Purpose |
Pagination |
less |
Browse large outputs |
Truncation |
head/tail |
Limit output size |
Counting |
wc |
Analyze stream contents |
Examples
## View first 10 lines
command | head -n 10
## View last 5 lines
command | tail -n 5
## Count lines, words, characters
command | wc
Combining Stream Operations
Pipe Chaining
## Complex stream processing
command | grep "pattern" | sed 's/x/y/' | sort | uniq
Error Handling and Logging
Selective Stream Management
## Suppress error output
command 2>/dev/null
## Log errors separately
command 2>> error.log
Stream Redirection Best Practices
- Use pipes for lightweight processing
- Avoid unnecessary stream manipulations
- Consider memory usage with large streams
LabEx Optimization Tip
In LabEx environments, be mindful of system resources when performing complex stream redirections.
Practical Use Cases
Log Analysis
## Real-world log processing example
cat /var/log/syslog | grep "error" | awk '{print $5}'
System Monitoring
## Capture system performance metrics
top -n 1 | head -n 5
Advanced Techniques
Process Substitution
## Compare outputs from different commands
diff <(command1) <(command2)
Tee Command
## Simultaneously display and save output
command | tee output.log