1

How could I interleave files line by line with

cmd.exe

with help of read, grep, echo, etc?

File1.txt with the context:

Aaaaaaa1 
Aaaaaaa2 
Aaaaaaa3

File2.txt with the context:

Bbbbbbb1
Bbbbbbb2
Bbbbbbb3

I would like to combine File1.txt and File2.txt in other file (OUTCOME.txt) in this way:

Aaaaaaa1
Bbbbbbb1
Aaaaaaa2
Bbbbbbb2
Aaaaaaa3
Bbbbbbb3
Community
  • 1
  • 1

1 Answers1

5

You need a method to read from two files in parallel. This is possible by using two methods at the same time (<file1 set /p and for /f ... in (file2)):

@echo off
setlocal enabledelayedexpansion

<file2.txt (
  for /f "delims=" %%a in (file1.txt) do (
    set /p b=
    echo %%a
    echo !b!
  )
) >outcome.txt
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Perhaps you should mention applicable limitations like line lengths or forbidden characters -- what do you think? – aschipfl Jan 08 '18 at 15:55
  • 1
    I don't think that it is necessary, as long as the answer works with the information provided then it is perfectly valid as is. If it doesn't then the OP is free to edit their currently off topic question to include fuller and more accurate details. – Compo Jan 08 '18 at 16:18
  • Clever: set /p + stdin redir – mojo Jan 08 '18 at 19:13
  • [Comparison of the lines with batch](https://stackoverflow.com/a/50068272/778560) – Aacini Jul 22 '22 at 23:22