使用powershell对CSV文件中的列进行排序

时间:2018-08-25 21:40:41

标签: powershell

我有一个1600行的csv文件,下面是前十行的内容

N,EQ,ADANIPORTS,ADANI PORT & SEZ LTD,384.5,385,387.8,375,376.75,792818726.1,2085488,Y, ,40850,452.35,350.45
N,EQ,ASIANPAINT,ASIAN PAINTS LIMITED,1394.75,1395,1411,1385.05,1393.5,1284559258,919355,Y, ,36117,1490.6,1090.1
N,EQ,AXISBANK,AXIS BANK LIMITED,631.75,638.05,643.4,634,639.9,9599936309,15035968,Y, ,144038,644.65,447.5
N,EQ,BAJAJ-AUTO,BAJAJ AUTO LIMITED,2685.55,2683.9,2697,2664,2682.25,1476618943,551229,Y, ,23611,3468.35,2605
N,EQ,BAJAJFINSV,BAJAJ FINSERV LTD.,7092.1,7092,7129,7025.25,7050.65,909166393.3,128111,Y, ,19707,7200,4500
N,EQ,BAJFINANCE,BAJAJ FINANCE LIMITED,2893.85,2892,2943.4,2891.05,2916.6,3884349778,1327710,Y, ,52356,2943.4,1511.2
N,EQ,BHARTIARTL,BHARTI AIRTEL LIMITED,369.9,370,370.8,365,368.95,768282183.8,2089422,Y, ,26515,564.8,331
N,EQ,BPCL,BHARAT PETROLEUM CORP LT,357.75,358.25,362,353.5,356.95,1738725370,4865929,Y, ,77863,551.55,353.5
N,EQ,CIPLA,CIPLA LTD,657.95,658,658,645,651.2,1235846442,1904031,Y, ,38575,665,507.2
N,EQ,COALINDIA,COAL INDIA LTD,289.05,287.85,293.6,287.8,291,791484837,2713583,Y, ,55421,316.95,235.85

我想按降序对10列进行排序,以便找出前20名。 文件名为 Pd240818.csv 我的powershell代码如下。

# To remove unwanted few lines

sls ",BE,",",EQ," .\Pd240818.csv | select -exp line | Where-Object {$_ -notmatch ',EQ, ,'} > .\temp.csv

#Sorting line is as follows

gc .\temp.csv | Where-Object {$_ -notmatch 'MKT,'}|%{$_.split(",")[9]}|Sort-Object -Descending| Select-Object -first 20 > temp.txt

排序

我得到temp.txt如下:

99988.7
99896.5
9989273.6
99769.75
996134.55
9933960.45
99228.65
99199.95
989418.15
988423057.7
9884111.1
98572145.2
982146.5
981497584.9
97982.75
9786178.9
9775915.05
9760482.5
97384498.85
971033.85

就像我在excel中对同一列进行排序一样,得到如下所示。

28818819313
9599936309
8459873415
6175554483
5889553012
5690666055
5439638100
5121938441
5079530750
5042021707
4972762046
4889394601
4742835986
3884349778
3690976213
3486309023
3388956937
3336437125
3206801588
3114870807

我在哪里做错了。如何纠正?

1 个答案:

答案 0 :(得分:5)

线索是看到不同长度的数字,这些数字全部排序在一起:

这是一个常见的问题,其中数字以文本而不是数字值的形式进行排序-当我们对单词进行排序时,不管它们有多长,我们将所有a放在一起,然后将所有{{ 1}}在一起...用数字来完成,然后将所有b放在一起,然后将所有9放在一起,就会看到这种长度可变的排序方式:

8

解决方案是将文本转换为数字,同时进行排序,然后它们将根据值进行排序:

99896.5
9989273.6
99769.75

然后输出更像您想要的:

.. | Sort-Object -Descending -Property { $_ -as [decimal] } | ..