Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何对文件中的字符串进行数字排序_Powershell_Batch File - Fatal编程技术网

Powershell 如何对文件中的字符串进行数字排序

Powershell 如何对文件中的字符串进行数字排序,powershell,batch-file,Powershell,Batch File,在我的批处理脚本中,将有一个临时文件,其中包含一个节中的注释(从json文件中获取,但这并不重要) 格式: (音符的位置(以毫秒为单位),(音符的种类),(如果不是0,则表示音符应该被保持,并且表示音符应该被保持的时间(以毫秒为单位) (格式不重要) 但有些时候它没有被分类 例如: 2285.71428571429,0,0 2367.85714285714,0,0 2455.35714285714,0,0 2576.78571428571,1,0 2757.14285714286,1,0 304

在我的批处理脚本中,将有一个临时文件,其中包含一个节中的注释(从json文件中获取,但这并不重要)

格式: (音符的位置(以毫秒为单位),(音符的种类),(如果不是0,则表示音符应该被保持,并且表示音符应该被保持的时间(以毫秒为单位)

(格式不重要)

但有些时候它没有被分类

例如:

2285.71428571429,0,0
2367.85714285714,0,0
2455.35714285714,0,0
2576.78571428571,1,0
2757.14285714286,1,0
3046.42857142857,0,0
2962.5,1,0
3128.57142857143,0,0
3216.07142857143,0,0
3298.21428571429,1,0
3380.35714285714,1,0
2669.64285714286,3,0
2882.14285714286,3,0


因此,我想对它们进行排序,但sort命令无法正确排序

预期成果:

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

行项目不需要排序。行需要排序。

您声明,格式不重要,所以我选择了表输出。鉴于您的输入似乎是csv格式,我们可以使用
powershell
导入csv
功能在
上拉入数据定界,然后使用
排序对象
对十进制数进行相应排序

在这里,我们将
Col1
的标题设置为
Col3
(根据您的示例),然后在
Col1
上排序,其余列按tact排序

$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
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
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
您可以通过以下方式直接从
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
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
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
或者混合到
批处理文件中
并将变量与
连接,以获得与输入相同的输出结果:

@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
结果:

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
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
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

最后两个数字并不重要,它需要对每一行进行排序。这确实有效,但这是我发现的:这确实有效,但这是我发现的:==================================Col1 Col2 Col3----------2285.71428571429 0 2367.85714285714 0 2455.35714285714 0 0 2576.78571428571 1 02669.64285714286 3 0 2757.14285714286 1 0 2882.14285714286 3 0... 很难读取,但是在col3中的一个数字中,有一个0也有一个“…”我可以注意到的是,有“0…”的行是文件中原始字符串的最后一行。原始文件的末尾也有空行,实际上没有任何作用。如果没有看到输入数据的实际示例,很难解释原因。如果您可以通过复制和粘贴来发布问题中的最后一部分数据,我可以看看原因。下面是powershell输入文件和输出的粘贴箱:好的,这是因为表格被截断,通过添加
| Format table-AutoSize-Wrap
来修复。请参见编辑。