如何使用 Powershell 对 CSV 数据进行排序?

How to sort CSV data with Powershell?

加载 CSV 文件后,如下所示,数据将如何排序?

PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> $words = Import-Csv -Path ./sort.csv
PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> $words                              

symbol code word  morse code phonetic
------ ---------  ---------- --------
A      Alfa/Alpha ● ▬        AL FAH
B      Bravo      ▬ ● ● ●    BRAH VOH
C      Charlie    ▬ ● ▬ ●    CHAR LEE
D      Delta      ▬ ● ●      DELL TAH
E      Echo       .●         ECK OH
F      Foxtrot    ● ● ▬ ●    FOKS TROT
G      Golf       ▬ ▬ ●      GOLF
Z      Zulu       ▬ ▬ ▬ ▬ ▬  ZOO LOO
H      Hotel      ● ● ● ●    HOH TELL
I      India      ● ●        IN DEE AH
J      Juliett    ● ▬ ▬ ▬    JEW LEE ETT
K      Kilo       ▬ ● ▬      KEY LOH
L      Lima       ● ▬ ● ●    LEE MAH
M      Mike       ▬ ▬        MIKE
N      November   ▬ ●        NO VEMBER
O      Oscar      ▬ ▬ ▬      OSS CAH
I      India      ● ●        IN DEE AH
P      Papa       ● ▬ ▬ ●    PAH PAH
Q      Quebec     ▬ ▬ ● ▬    KEH BECK
S      Sierra     ● ● ●      SEE AIRRAH
R      Romeo      ● ▬ ●      ROW ME OH
T      Tango      ▬          TANG OH
U      Uniform    ● ● ▬      YOU NEE FORM
V      Victor     ● ● ● ▬    VIK TAH
W      Whiskey    ● ▬ ▬      WISS KEY
X      X-ray      ▬ ● ● ▬    ECKS RAY
Y      Yankee     ▬ ▬ ● ●    YANG KEY
Z      Zulu       ▬ ▬ ▬ ▬ ▬  ZOO LOO

PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> $words | sort 'code word'           
/usr/bin/sort: cannot read: 'code word': No such file or directory
PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> sort $words              
/usr/bin/sort: cannot read: '@{symbol=A; code word=Alfa/Alpha; morse code=● ▬; phonetic=AL FAH}': No such file or directory
PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> help sort
PS /home/nicholas/powershell/csv>              

另见:

https://devblogs.microsoft.com/scripting/use-powershell-to-sort-csv-files-in-order/

它有类似的例子,只是数据按照导入的方式排序。


原始数据为:

nicholas@mordor:~/powershell/csv$ 
nicholas@mordor:~/powershell/csv$ head sort.csv 
symbol,code word,morse code,phonetic
A,Alfa/Alpha,● ▬,AL FAH
B,Bravo,▬ ● ● ●,BRAH VOH
C,Charlie,▬ ● ▬ ●,CHAR LEE
D,Delta,▬ ● ●,DELL TAH
E,Echo,.●,ECK OH
F,Foxtrot,● ● ▬ ●,FOKS TROT
G,Golf,▬ ▬ ●,GOLF
Z,Zulu,▬ ▬ ▬ ▬ ▬,ZOO LOO
H,Hotel,● ● ● ●,HOH TELL
nicholas@mordor:~/powershell/csv$ 

copy/pasted 来自网站。


获取会员显示:

PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> $words = Import-Csv -Path ./sort.csv
PS /home/nicholas/powershell/csv> 
PS /home/nicholas/powershell/csv> $words | Get-Member                 

   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
code word   NoteProperty string code word=Alfa/Alpha
morse code  NoteProperty string morse code=● ▬
phonetic    NoteProperty string phonetic=AL FAH
symbol      NoteProperty string symbol=A

PS /home/nicholas/powershell/csv> 

符合预期。

sort 是 Linux 系统上的外部命令(应用程序)。
换句话说,不要使用短名称 (sort),而是使用完整的 cmdlet 名称 Sort-Object:

$Words |Sort-Object 'code word'

正如 @postanote, details about the command precedence can be found in about_Command_Precedence 评论的那样。阅读本文,实际上可能看起来很混乱,因为该过程被定义为按以下顺序发生:Alias, Function, Cmdlet, any external command/script(这应该给予 alias 优先级通过 外部命令 sort).
显然 sort 别名根本没有安装在 Linux 系统上(大概是为了防止它否决外部本地 Linux 命令):

Windows (10)

Get-Command sort

CommandType     Name                                       Version    Source
-----------     ----                                       -------    ------
Alias           sort -> Sort-Object

Linux (圆周率 OS)

Get-Command sort

CommandType     Name                                       Version    Source
-----------     ----                                       -------    ------
Application     sort                                       0.0.0.0    /usr/bin/sort

这意味着您也可以通过使用 sort 别名手动否决外部 sort 命令来解决此问题:

Set-Alias sort Sort-Object