Milton T. Machado S
Milton T. Machado S

Reputation: 181

Awk command to Powershell equivalent

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} '

What his does is print lines that contain the string Box11, then piping it to another awk that prints the total of the 4th column (delimited by spaces).

Upvotes: 17

Views: 67574

Answers (6)

js2010
js2010

Reputation: 27516

Need powershell 7 for the measure-object scriptblock.

'"Box11" 2 3 4
"Box11" 2 3 4' | set-content test.txt
select-string Box11 test.txt | measure -sum { (-split $_.line)[3] }

Count             : 2
Average           :
Sum               : 8
Maximum           :
Minimum           :
StandardDeviation :
Property          :  (-split $_.line)[3]

Upvotes: 0

the-citto
the-citto

Reputation: 137

in Windows Git installs awk, grep, and much more, in its \usr\bin folder

just added it to my path

Upvotes: -1

Juliano Marcon
Juliano Marcon

Reputation: 11

I prefer to use Scoop to install command line tools like "awk".

For windows, you can use gawk that can be installed with Chocolatey like in @MartinThé aswer.

I prefer to use Scoop.

Upvotes: 0

rayiik
rayiik

Reputation: 139

I know this post is old, but I thought I'd add to this. Currently, if you have WSL (windows sub system for Linux) enabled, (windows 10 all version on systems that support virtualization, in the turn windows features on) with a distribution installed in the subsystem. You can can call Linux commands directly from windows

wsl -e awk '/"Box11"/{sum += $4} END{print sum}' test.txt

(borrowed from @Ed Moritn) ( or any awk command of your choice. ) Basically cmd or PowerShell takes the command and pipes it into the subsystem and the results are returned (bit of an over simplification, but in effect accurate). But the -e flag allows you to execute the command without opening an instance.

edit Since writing this initial response, I have found two answers which are better solutions. The first is GNUwin32 This is a collection of Gnutils which have been ported to windows standalone .exe files including sed, awk, grep and many more, allowing you to call get-childitem | awk.exe '{print $1}' directly. These tools are fully portable with no installation required. The second option is Msys32, a platform that grew out of chocolatey (though it is almost fully code in dependant now) designed for cross compiling binaries. Once installed in the /bin folder, are many Linux utilities as exe files. most of these executable can be pulled from the bin and are portable with no required installation of dependencies. The reason msys32 is preferred (in my books) over the gnuwin32 is the fact that that gnuwin32 has gawk version 3.1 and msys32 has nawk and gawk vs 5.1.

Upvotes: 11

MartinThé
MartinThé

Reputation: 736

You can get get awk for Windows now. I have been using it as a direct replacement and haven't had any problems yet.

It can be easily installed via Chocolatey

Upvotes: 1

Matt
Matt

Reputation: 46730

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 " "

Upvotes: 29

Related Questions