问题
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