1

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?

4
  • What operating system are you using? Commented May 24, 2018 at 1:22
  • OS is Windows 7. The paste, diff, and cut are GNU stuff for win32 (probably found them here years ago I think? - they're sooo great!!!) Commented May 24, 2018 at 16:13
  • Don't know much about Windows or if the new Win10 bash can be installed on Win7, but if/when Windows gets clever, you may be able to do paste <(myCommand ... 10-20) <(myCommand ... 10-20). Commented May 25, 2018 at 6:36
  • Yes, Mark! That's very much what I'm looking for (see edit/clarification above). Your suggestion resembles what I want but upon execution I get "The system cannot find the file specified" Is there a different syntax that can do this? Commented May 25, 2018 at 14:10

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

9 Comments

Hmmm. If you'd like to understand "why", perhaps you should phrase it as a question rather than simply lament a perception of yours (perhaps indefinetly?). Regarding the 'beginners', I'm not sure if that's intended to be insulting but I will say I am of an age that I very well may have been computing longer than you. Either way, it seems inappropriate to me and your suggestion is not helpful. This just pipes the output of one command to the next, and then to the next, finally putting the output of the last command in temp.txt.
OK, sorry, didn't see the ampersand. Still though, it just "stacks" the output. I'd like the first row of each command to be on the first line, the second line of each on the second, ... so on.
That would require to run both commands synchronized, that's not possible. You have to cache both outputs (into files?) and then use a script to merge the two files - but that's definitively no oneliner. (btw: no offense intended; I just noticed, that especially "low-reps" tend to request one-liners - and hey, we are talking about scripts; keep them readable, no reason for code-golfing). You should add an example to your question (input, desired output) to make clear, what you expect.
I retract from "definitively no onliner" :) See my edit.
That's a very clever way to avoid using paste! Fortunately I do have paste on my system (and stand back, I'm not afraid to use it!) Unfortunately that also doesn't help for using diff on the same example. Please see edit/clarification above as I realize I wasn't very clear about what I'm looking for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.