请求csv的Powershell脚本,类型为

时间:2016-07-14 19:30:53

标签: csv powershell sql-order-by

有一个像这样的csv。很想使用powershell Import-csv

我需要每种类型水果的最后(或最新)3条记录。

#foo.csv
No, Fruit, Quality, Units, Location
1, Orange, New, 10, Village
2, Orange, Fresh, 20, City
3, Apple, New, 15, Village
4, Grapes, Fresh, 25, City
5, Apple, Fresh, 30, City2
6, Apple, Fresh, 35, City1
7, Apple, Fresh, 35, City2
8, Grapes, Fresh, 30, City2
9, Grapes, Fresh, 35, City3

输出将包含Apples的行5,6,7和Grapes的4,8,9行,以及橙子的行1,2等等。排序需要通过水果然后行id。

1 个答案:

答案 0 :(得分:2)

$data = import-csv D:\fruits.txt
$data | group Fruit | foreach {
    $_.group | select -last 3
} | sort Fruit | ft -AutoSize

e.g。输出:

No Fruit  Quality Units Location
-- -----  ------- ----- --------
5  Apple  Fresh   30    City2   
6  Apple  Fresh   35    City1   
7  Apple  Fresh   35    City2   
4  Grapes Fresh   25    City    
8  Grapes Fresh   30    City2   
9  Grapes Fresh   35    City3   
1  Orange New     10    Village 
2  Orange Fresh   20    City