您说,格式并不重要所以我选择了表格输出。看到您的输入似乎是 csv 格式,我们可以使用powershell 的Import-Csv 函数将, 上的数据分隔符拉入,然后使用Sort-Object 对十进制数进行相应的排序。
在这里,我们将Col1 的标题设置为Col3(根据您的示例),然后对Col1 进行排序,其余列保持不变。
$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt
$file | Sort-Object { [double]$_.Col1 } | Format-Table -AutoSize -Wrap
结果:
PS Z:\> $file | Sort-Object -Descending { [double]$_.Col1 } | Format-Table -AutoSize -Wrap
Col1 Col2 Col3
---- ---- ----
2285.71428571429 0 0
2367.85714285714 0 0
2455.35714285714 0 0
2576.78571428571 1 0
2669.64285714286 3 0
2757.14285714286 1 0
2882.14285714286 3 0
2962.5 1 0
3046.42857142857 0 0
3128.57142857143 0 0
3216.07142857143 0 0
3298.21428571429 1 0
3380.35714285714 1 0
您可以直接从cmd 运行:
powershell "$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt; $file | Sort-Object { [double]$_.Col1 } | Format-Table -AutoSize -Wrap"
结果:
PS Z:\> $file | Sort-Object -Descending { [double]$_.Col1 } | Format-Table -AutoSize -Wrap
Col1 Col2 Col3
---- ---- ----
2285.71428571429 0 0
2367.85714285714 0 0
2455.35714285714 0 0
2576.78571428571 1 0
2669.64285714286 3 0
2757.14285714286 1 0
2882.14285714286 3 0
2962.5 1 0
3046.42857142857 0 0
3128.57142857143 0 0
3216.07142857143 0 0
3298.21428571429 1 0
3380.35714285714 1 0
或混合到batch-file 并使用, 连接变量,为您提供与输入相同的输出结果:
@for /F "skip=3 tokens=1-3" %%i in ('powershell "$file = Import-Csv -Header "Col1", "Col2", "Col3" -delimiter ',' test.txt; $file | Sort-Object { [double]$_.Col1 }"') do @echo(%%i,%%j,%%k
结果:
Z:\>sort_object-Int.cmd
2285.71428571429,0,0
2367.85714285714,0,0
2455.35714285714,0,0
2576.78571428571,1,0
2669.64285714286,3,0
2757.14285714286,1,0
2882.14285714286,3,0
2962.5,1,0
3046.42857142857,0,0
3128.57142857143,0,0
3216.07142857143,0,0
3298.21428571429,1,0
3380.35714285714,1,0