Advanced Stream Control
Stream Processing and Piping
Advanced stream control enables complex data manipulation through command chaining and sophisticated input/output processing techniques.
Piping Mechanisms
graph LR
A[Command 1] --> B[Pipe |]
B --> C[Command 2]
C --> D[Command 3]
| Piping Technique | Description | Example |
| -------------------- | --------------------------------- | ---------------------- | ---------- | ----- |
| Simple Piping | Transfers output between commands | ls | grep .txt
|
| Multi-stage Piping | Chains multiple commands | cat file.log | sort | uniq
|
| Process Substitution | Treats command output as file | diff <(cmd1) <(cmd2)
|
Text Processing with Streams
## Filter lines containing specific pattern
cat data.txt | grep "error"
## Count occurrences
ps aux | wc -l
## Transform and filter data
cat access.log | awk '{print $1}' | sort | uniq -c
Subprocess Stream Management
Redirecting Subprocess Streams
import subprocess
## Capture subprocess output
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
print(result.stdout)
## Handle error streams
try:
subprocess.run(['invalid_command'], check=True)
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr}")
Advanced stream control provides powerful mechanisms for data processing, enabling complex transformations and analysis through command-line and programmatic techniques.