简体   繁体   中英

Linux Bash Shell Command in Windows Powershell

So the following command in Linux is to order a Nginx access.log file by those making the most requests.

'awk '{ print $1 }' access.log | uniq -c | sort -nr | more'

What it the equivalent for this command in Windows Powershell ?

Get-Content access.log | ForEach-Object { $_.split()[0] -as [IPAddress] } | Sort-Object | Select-Object -Unique -ExpandProperty IPAddressToString

or

gc access.log |%{ $_.Split()[0] -as [IPAddress] } | sort -U |%{ "$_" }
  1. Read the file
  2. Process it line by line
  3. Split on spaces and take the first element
  4. Cast it to an IPAddress type so it will sort numerically
  5. Sort and deduplicate one way or another
  6. Get the string representation of the [IPAddress] back out

NB. your code doesn't do what you claim; you need to be sorting first, before uniq , as it only removes consecutive duplicates, not all duplicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM