Is it possible using CMD and Powershell to combine 2 files into 1 file like this:
file1-line1 tab file2-line1 file1-line2 tab file2-line2 file1-line3 tab file2-line3
So it takes file 1 line 1 and the inserts a tab and then inserts file 2 line 1. Then does this for all subsequent lines in each file?
A generalized solution supporting multiple files, building on Ansgar Wiechers' great, memory-efficient
System.IO.StreamReader
solution:PowerShell's ability to invoke members (properties, methods) directly on a collection and have them automatically invoked on all items in the collection (member enumeration, v3+) allows for easy generalization:
Get-MergedLines
(source code below; invoke with-?
for help) wraps the functionality in a function that:accepts a variable number of filenames - both as an argument and via the pipeline
uses a configurable separator to join the lines (defaults to a tab)
allows trimming trailing separator instances
Probably the simplest solution is to use a Windows port of the Linux
paste
utility (e.g.paste.exe
from the UnxUtils):From the man page:
For a PowerShell(ish) solution, I'd use two stream readers:
This avoids having to read the two files entirely into memory before merging them, which bears the risk of memory exhaustion for large files.
Further details at this post
Powershell solution:
There are a number of recent locked [duplicate] questions that link into this question like:
were I do not agree with because they differ in a way that this question concerns text files and the other concern
csv
files. As a general rule, I would advice against manipulating files that represent objects (likexml
,json
andcsv
). Instead, I recommend to import these files (to objects), make the concerned changes and ConvertTo/Export the results back to a file.One example where all the given general solutions in this issue will result in an incorrect output for these "duplicates" is where e.g. both
csv
files have a common column (property) name.The general
Join-Object
(see also: In Powershell, what's the best way to join two tables into one?) will join two objects list when the-on
parameter is simply omitted. Therefor this solution will better fit the other (csv
) "duplicate" questions. Take Merge 2 csv files in powershell [duplicate] from @Ender as an example:In comparison with the "text" merge solutions given in this answer, the general
Join-Object
cmdlet is able to deal with different file lengths, and let you decide what to include (LeftJoin
,RightJoin
orFullJoin
). Besides you have control over which columns you what to include ($A | Join $B -Property ID, Name
) the order ($A | Join $B -Property ID, Class, Name
) and a lot more which cannot be done which just concatenating text.Specific to this question:
As this specific question concerns text files rather then
csv
files, you will need to ad a header (property) name (e.g.-Header File1
) while imparting the file and remove the header (Select-Object -Skip 1
) when exporting the result:In PowerShell, and assuming both files have exactly the same number of lines: