试试这个呢(管道+自定义对象)
(
“My name is OceanHo”,
“My name is ZhangSan”,
“My name is LiSi”,
“My name is WangErMaZi”
) | ForEach-Object -Process {
Return Get-KeyValuePSObject -Text $_ -SortKeyStartPos 11
} | Sort-Object -Property Key | Out-File -FilePath “F:\my-sorted.txt”
试试这个呢(管道+自定义对象)
(
“My name is OceanHo”,
“My name is ZhangSan”,
“My name is LiSi”,
“My name is WangErMaZi”
) | ForEach-Object -Process {
Return Get-KeyValuePSObject -Text $_ -SortKeyStartPos 11
} | Sort-Object -Property Key | Out-File -FilePath “F:\my-sorted.txt”
Function Get-KeyValuePSObject{
Param(
[string]$Text,
[int]$SortKeyStartPos = 0,
[int]$SortKeyFinishedPos = -1
)
$SortTemplate = (” | Select-Object Key,Value )
If($Text.Length -eq 0){
Return
}
If($SortKeyFinishedPos -le -1){
$SortKeyFinishedPos = $Text.Length
}
$SortTemplate.Key = $Text.Substring($SortKeyStartPos,($SortKeyFinishedPos – $SortKeyStartPos))
$SortTemplate.Value = $Text
Return $SortTemplate
}
先谢谢啦,
不过觉得太麻烦了,下面的如何呢
Get-Content “d:\a.txt” | Sort-Object { [String]$_.substring(1,1)}, { [String]$_.substring(6) } | set-Content d:\b.txt