3 ms·
I was teaching a class on algorithms to some engineering students. They needed to sort a file of random integers using a few different algorithms. This is the
by slavik81 8y ago
I was teaching a class on algorithms to some engineering students. They needed to sort a file of random integers using a few different algorithms.
This is the bash I wrote to check their results were sorted properly on Linux:
diff <(cat output.txt) <(cat input.txt | sort -n)
This is the PowerShell I wrote to check their results were sorted properly on Windows:
cat input.txt | foreach-object { [Int] $_ } | sort-object | out-file -encoding ascii sorted.txt
diff (cat output.txt) (cat sorted.txt)
The casting felt a little painful, but it seemed straightforward. The sorted result was generated correctly.
Unfortunately, I was surprised to discover that PowerShell's diff found no differences between a sorted file of numbers and the unsorted file of numbers.
- AnIdiotOnTheNet 8y agoThat's because `diff` is an alias of `Compare-Object`, which works radically differently than UNIX `diff` because of the defaults. In some circumstances you could see how this behavior would be more useful, though obviously not this one. For reference, `Compare-Object -SyncWindow 0` would give the behavior you wanted. The default is [Int32]::MaxValue. Aliasing UNIX command names is another one of PowerShell's mistakes in my opinion, because it creates the expectation that they work the same way when they often very much don't. Ultimately rather than help UNIX users, as was probably intended, this just confuses and annoys them.
- yjftsjthsd-h 8y ago> rather than help UNIX users, as was probably intended, this just confuses and annoys them. Compatibility layers are great, when they're actually compatible. Almost-compatible layers are a nightmare.
- wolfgke 8y ago> Compatibility layers are great, when they're actually compatible. Almost-compatible layers are a nightmare. That's why you should not use them. You decided to use them.
- yjftsjthsd-h 8y agoWell, perhaps some hypothetical "you" uses them:) To be clear, I run 100% genuine Bourne-family shells on unix-like systems, and merely visit MS-land for sightseeing;)
- slavik81 8y agoI wasn't just blindly assuming the alias worked the same. I actually read the Compare-Object documentation [1], which contains that exact command as an example of how to compare files. I just didn't understand the sort of comparison it did. If you search "PowerShell compare two files", it becomes pretty clear this is a common confusion even among people using the name Compare-Object. For example, the top serverfault question has an incorrect top-voted, accepted answer [2]. [1]: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-6#examples https://docs.microsoft.com/en-us/powershell/module/microsoft... [2]: https://serverfault.com/a/5604 https://serverfault.com/a/5604