按文件中的第一列排序

时间:2018-12-04 20:32:44

标签: powershell sorting

我有一个名为adat.dat的文件,其中包含以下数据:

1;400;1000000;garden kitchen
11;178;56124;bathroom roof
7;777;20000;oneroom kitchen
10;150;1000000;garage yard

我想按第一列从最低到最高排序。

所以预期的输出将是:

1;400;1000000;garden kitchen
7;777;20000;oneroom kitchen
10;150;1000000;garage yard
11;178;56124;bathroom roof

到目前为止,这是我的代码:

Get-Content adat.dat | Sort-Object 

是否有类似于Linux sort -n选项的选项?

2 个答案:

答案 0 :(得分:4)

使用int[,] graph = new int[totalNumberOfCaves, totalNumberOfCaves]; for (int i = 0; i < graph.GetLength(0); i++) { for (int j = 0; j < graph.GetLength(1); j++) { graph[i, j] = connectionWeight["Value at possition"]; } } 读取数据。如果数据没有标题,则可以通过Import-Csv参数指定自己的标题。然后使用自定义属性按第一列进行排序,该属性将字符串值转换为整数(用于数字排序)。

-Header

答案 1 :(得分:2)

这是一种使用=AVERAGEIFS(L6:L205, D6:D205, {"W","R"}, C6:C205, "Cache") 而不是Get-Content的方法。对于大文件,Import-CSV中的对象数组可能会变得很大。 [ grin ]通过使用脚本块为Import-CSV目标创建计算所得的属性,从而避免了某些情况。

Sort-Object

输出...

# fake reading in a text file
#    in real life, use Get-Content
$InStuff = @'
1;400;1000000;garden kitchen
11;178;56124;bathroom roof
7;777;20000;oneroom kitchen
10;150;1000000;garage yard
'@ -split [environment]::NewLine

$SortedInStuff = $InStuff |
    Sort-Object {[int]$_.Split(';')[0]}

$SortedInStuff
相关问题