Basic Usage
- Piping Between Commands:
1
command1 | command2
Output of
command1
is used as input forcommand2
.
Combining Multiple Commands
- Sequential Commands:
1
command1 | command2 | command3
Output of
command1
is passed tocommand2
, and its output is passed tocommand3
.
Filtering and Processing Text
- Grep:
1
command | grep 'pattern'
Filters the output of
command
for lines containing ‘pattern’. - Sort:
1
command | sort
Sorts the output of
command
. - Awk:
1
command | awk '{print $1}'
Processes the output of
command
usingawk
(e.g., prints the first column). - Sed:
1
command | sed 's/old/new/'
Processes the output of
command
usingsed
(e.g., replaces ‘old’ with ‘new’).
Redirection and File Operations
- Tee:
1
command | tee file.txt
The output of
command
is both displayed and written tofile.txt
.
Combining Text Streams
- Paste:
1
command1 | paste - command2
Combines the output of
command1
andcommand2
side by side.
Networking and Data Transfer
- Curl and Processing:
1
curl -s http://example.com | grep 'title'
Fetches data from a URL and filters it.
- SSH and Remote Commands:
1
ssh user@host 'cat file' | grep 'pattern'
Executes a command on a remote machine and processes the output locally.
Advanced Usage
- Process Substitution:
1
diff <(command1) <(command2)
Compares the output of two commands using
diff
. - Xargs for Batch Processing:
1
command1 | xargs -n1 command2
Uses the output of
command1
as arguments forcommand2
, one at a time.
Debugging and Analysis
- Pipe Viewer (pv):
1
command1 | pv | command2
Monitors the progress of data through a pipe between two commands.
Chains with Conditional Execution
- AND and OR Lists:
1 2
command1 && command2 | command3 command1 || command2 | command3
Executes
command2
only ifcommand1
succeeds/fails, then pipes tocommand3
.