Awk command to Powershell equivalent

╄→гoц情女王★ 提交于 2020-04-11 12:27:50

问题


I hope can you help me, essentially, I'm looking for the Powershell equivalent of the awk command:

awk '/"Box11"/ { print $0 }' test.txt|awk '{ SUM += $4} END { print SUM} '

回答1:


Multiple ways of doing it but this would do the trick:

Get-Content c:\temp\test.txt | Where-Object{$_ -match '"Box11"'} |
  ForEach-Object{($_ -split "\s+")[3]} | Measure-Object -Sum |
  Select-Object -ExpandProperty Sum

Get a string array of the file. For each line that contains the string "Box11" we split the line on each group of spaces. Then pass the 4 element of each match to Measure-Object.

A short hand, if you value that, would look like this:

gc c:\temp\test.txt | ?{$_ -match '"Box11"'} | %{($_ -split "\s+")[3]} |
  Measure -Sum | Select -Exp Sum

If this file/string input had header this would be a good start as well. Assuming of course that your file is delimited with one space exactly.

Get-Content c:\temp\test.txt | ConvertFrom-Csv -Delimiter " "


来源:https://stackoverflow.com/questions/30110707/awk-command-to-powershell-equivalent

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