How to read a file word by word in power shell?

陌路散爱 提交于 2020-12-27 06:37:20

问题


In this case I am getting an error.

Anyone has any idea what's the problem in the code is?


回答1:


param(
    $fileName = "d:\tmp\file.txt",
    $name = "Alan"
)
@'
Mohammed 3 4 5 4

Alan 2 1 3 2

Li 3 5 1 3
'@ | Out-File $fileName -Encoding default

$hash = Get-Content $fileName | Where-Object {$_} | ForEach-Object {
    $array = $_ -split "\s+"
    @{
        $array[0] = [Linq.Enumerable]::Average([int[]]($array[1..($array.Count-1)]))  
    }
} 

if ($hash.Keys -contains $name){
    return "$name->$($hash.$name)"
}
else {
    return "$name=>no $name"
}


来源:https://stackoverflow.com/questions/65074354/how-to-read-a-file-word-by-word-in-power-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!