Pipelines allow you to pass the output of one command as input to another, enabling efficient data processing. This cheatsheet provides examples of sorting, filtering, and presenting data using pipelines in Bash and PowerShell.
cat file.txt | sort
Sorts the contents of file.txt
alphabetically.
cat file.txt | grep "pattern"
Filters lines in file.txt
that match the specified pattern.
ls -l | awk '{print $9, $5}'
Displays the file name and size from the ls -l
command output.
Get-Content file.txt | Sort-Object
Sorts the contents of file.txt
alphabetically.
Get-Content file.txt | Where-Object { $_ -match "pattern" }
Filters lines in file.txt
that match the specified pattern. You can also check for a specific string using findstr (findsrt is a Windows command-line utility):
Get-ChildItem | findstr "pattern"
Get-ChildItem | Select-Object Name, Length
Displays the file name and size from the Get-ChildItem
command output.
Get-ChildItem | Select-Object Name, Length | Format-Table -AutoSize
displays the file name and size from the Get-ChildItem
command output in a table format. you can also use Format-List
to display the output in a list format:
Get-ChildItem | Select-Object Name, Length | Format-List
Get-ChildItem | Select-Object Name, Length | Out-GridView
This command displays the file name and size from the Get-ChildItem
command output in a grid view, allowing for easier data exploration.
Get-ChildItem | Select-Object Name, Length | ConvertTo-Html -Fragment | Out-File output.html
This command converts the output of Get-ChildItem
to HTML format and saves it to output.html
. You can also use ConvertTo-Json
to convert the output to JSON format:
Get-ChildItem | Select-Object Name, Length | ConvertTo-Json | Out-File output.json
This command converts the output of Get-ChildItem
to JSON format and saves it to output.json
.
Pipelines are powerful tools for chaining commands and processing data efficiently. Use these examples as a starting point to build more complex workflows.