Windows下如何用单行命令实现双命令输出的paste/diff操作?
Absolutely! You can absolutely achieve this on Windows without temporary files and with concise syntax similar to Unix command substitution—no need to chain commands with & or create temp files. The approach depends on which shell you're using: Unix-like shells on Windows (like Git Bash, WSL, or Cygwin) give you near-identical syntax to Unix, while native Command Prompt (CMD) has a workaround, and PowerShell offers its own clean alternative.
This is the simplest option if you have access to these tools, as they support process substitution (<()) just like Unix shells—exactly what you're looking for.
1. Merge Outputs with paste
Use process substitution to feed the output of both commands directly into paste without temp files:
paste <(myCommand options1 | cut -c 10-20) <(myCommand options2 | cut -c 10-20)
The <() syntax runs each command inside and passes its output as a "virtual file" to paste, just like using temp files but without the cleanup.
2. Compare Outputs with diff
Same logic applies for diff—just swap paste with diff:
diff <(myCommand options1 | cut -c 10-20) <(myCommand options2 | cut -c 10-20)
If you prefer using Windows' built-in PowerShell, you can use subexpressions ($()) and PowerShell-native cmdlets to replicate the behavior. Note: PowerShell doesn't include paste or cut by default, so we'll use equivalent cmdlets:
1. Merge Outputs (Equivalent to paste)
Use Select-Object-style string manipulation to mimic cut -c 10-20 (skip first 9 characters, take next 11), then pair lines in a single line command:
$o1=myCommand options1|%{$_.Substring(9,11)};$o2=myCommand options2|%{$_.Substring(9,11)};for($i=0;$i-lt$o1.Count;$i++){"$($o1[$i])`t$($o2[$i])"}
2. Compare Outputs with diff
If you have diff available (e.g., via Git Bash), you can use process substitution in PowerShell too:
diff <(myCommand options1 | ForEach-Object { $_.Substring(9, 11) }) <(myCommand options2 | ForEach-Object { $_.Substring(9, 11) })
Or use PowerShell's native Compare-Object for a fully Windows-native solution:
Compare-Object -ReferenceObject (myCommand options1 | %{$_.Substring(9,11)}) -DifferenceObject (myCommand options2 | %{$_.Substring(9,11)})
CMD doesn't support process substitution, but you can use nested for /f loops to capture each command's output and combine/compare them in one line:
1. Merge Outputs (Simulate paste)
for /f "delims=" %a in ('myCommand options1 ^| cut -c 10-20') do @for /f "delims=" %b in ('myCommand options2 ^| cut -c 10-20') do @echo %a %b
Note: The ^ escapes the pipe | for the inner commands, and @ suppresses command echo. This pairs each line from the first command with the corresponding line from the second.
2. Compare Outputs with diff
If diff is available in your PATH (e.g., from Git Bash), you’ll get the cleanest results by running the Unix-like shell syntax directly in that shell instead of vanilla CMD.
内容的提问来源于stack exchange,提问作者Keith M

