Keith M
Keith M

Reputation: 27

redirecting output of two commands

I'm wondering if there's a one-liner to do the following on Windows:

myCommand options1 | cut -c 10-20 > temp1.txt
myCommand options2 | cut -c 10-20 > temp2.txt
paste temp1.txt temp2.txt

I'd also like to do similar using diff instead of paste but I'm guessing the answer would be the same (yes?). In general temp1 and 2 will both have more than one line of text. I found the following which looks like a similar question Print output of two commands in two columns in excel file but that doesn't have an answer.

Edit/Update: I realize now that I wasn't very clear about the 'one-liner'. Obviously I can put all three lines together on one line by simply separating them with ampersands. That's not what I'm looking for. I vaguely recall from using Unix many years ago that backticks could be used for things like this. Maybe something like

paste `cmd1` `cmd2`

not really too sure(?). That's the sort of thing I'm looking for (on windows). Ideally it would not involve (much) more typing than simply entering the three lines above. Does this exist?

Upvotes: 1

Views: 668

Answers (1)

Stephan
Stephan

Reputation: 56228

I'll never understand why one wants to use one-liners, but here you go:

(ping localhost |find "Mini" & ipconfig |find "IPv4")>temp.txt

edit to show output of both commands side by side:

@echo off
setlocal enabledelayedexpansion
ping localhost |find "TTL" >1.txt
ipconfig |find ":" >2.txt

<1.txt (
  for /f "delims=" %%a in (2.txt) do (
    set /p "x="
    echo !x! ; %%a
    set "x=-"
  )
)>temp1.txt

same as one-liner (as batchfile):

cmd /v:on /c ping localhost |find "TTL" >1.txt & ipconfig |find ":" >2.txt & <1.txt (for /f "delims=" %%a in (2.txt) do (set /p "x=" & echo !x! ; %%a &  set "x=-"))>temp2.txt

one-liner to be used on command line:

cmd /v:on /c "ping localhost |find "TTL" >1.txt & ipconfig |find ":" >2.txt & <1.txt (for /f "delims=" %a in (2.txt) do @(set /p "x=" & echo !x! ; %a &  set "x=-"))>temp.txt"

Note: in the result there are as much lines as in 2.txt (output of the second command). If 1.txt has more lines, they are omitted (less lines are not a problem)

Upvotes: 1

Related Questions