Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.4k views
in Technique[技术] by (71.8m points)

linux - How to sort CSV data with Powershell?

Once a CSV file is loaded, as below, how is the data then sorted?

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>              

see also:

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

which has similar examples, except that the data is sorted as its imported.


original data as:

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$ 

which was copy/pasted from a website.


get members shows:

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> 

which is as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

sort is an external command (Application) on Linux systems.
In other words, do not use the short name (sort) but the full cmdlet name Sort-Object:

$Words |Sort-Object 'code word'

As commented by @postanote, details about the command precedence can be found in about_Command_Precedence. Reading this, might actually look confusing as the process is defined to occur in this order: Alias, Function, Cmdlet, any external command/script (which should give the alias precedence over the external command sort).
Apparently the sort alias is simply not installed on Linux systems (presumably to prevent it from overruling the external native Linux command):

Windows (10)

Get-Command sort

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

Linux (Pi OS)

Get-Command sort

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

This means that you might also resolve this issue by manually overruling the external sort command with a sort alias:

Set-Alias sort Sort-Object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...