psg*_*g20 2 powershell powershell-4.0
我的 txt 文件中有以下内容,该文件将由 powershell 读取,并应根据自定义条件重新排列
文件:abc.txt
General
User1/Alert
User1/LogicApp
General/Abc
User2/Alert
User2/LogicApp
Run Code Online (Sandbox Code Playgroud)
上面应该按自定义条件排序,如果“General”存在,它应该位于列表顶部,如果字符串“LogicApp”存在,它应该位于 General 旁边。因此,输出应该类似于下面的内容
General
General/abc
User1/LogicApp
User2/LogicApp
User1/Alert
User2/Alert
Run Code Online (Sandbox Code Playgroud)
如果字符串“General”不存在,则字符串“LogicApp”应优先。我们可以在 Powershell 中进行这种自定义排序吗?
-Property您可以使用参数 of执行自定义排序Sort-Object,该参数接受多个表达式。
'General','User1/Alert','User1/LogicApp','General/Abc','User2/Alert','User2/LogicApp' |
Sort-Object -Property @{Expression={$_ -like '*General*'}; Descending=$true}, {if($_ -like '*LogicApp*') {0} else {1}}, {$_}
Run Code Online (Sandbox Code Playgroud)
General 和 LogicApp 使用不同的方法排序只是为了说明。最后一个元素{$_}确保按字母顺序排序。
| 归档时间: |
|
| 查看次数: |
58 次 |
| 最近记录: |