Any way to do Linux-style sort --merge in Windows PowerShell?

1

In PowerShell, if I have two files that are already sorted and want to merge them into one big sorted file, it seems wasteful and inefficient to do gc file1, file2 | sort-object > newfile, especially when the files are very large.

How to tell PowerShell to make use of the fact that the input files are already sorted?

ghostarbeiter

Posted 2016-05-04T19:23:04.200

Reputation: 131

Could you add an example of your linux sort command? Otherwise, it's not entirely clear, what are you want to do. Skip Sort-Object and redirect directly to a new file?

– beatcracker – 2016-05-04T21:13:01.540

@beatcracker, suppose file1 contains { A C D F J L M N } and file2 contains { B E G H I K O }, then the merged files should contain { A B C D E F G H I J K L M N O }. Pretend each letter in the previous example represents a line in a file. What I'd like is a way to tell sort-object (or some other cmdlet if it exists) that the input files are already sorted, so it should just efficiently merge the two files rather than just concatenating them and then sorting that. The files in question are rather large (a million lines or so each). – ghostarbeiter – 2016-05-04T21:20:02.103

I'm not sure, but it looks like you want to do a merge sort which Linux sort can do like this. There is some PS implementations available: 1, 2, 3. Start with a first one: it doesn't use += operator as other two, so it should be faster.

– beatcracker – 2016-05-04T22:07:11.180

No answers