user3022917
user3022917

Reputation: 617

Select column and exact string match from binary output

I'm looking for an equivalent command in PowerShell:

Linux/macOS:

cat mmls.txt | awk '{print $3}' | egrep -o '[0,9]{10}' | awk 'NR==1' | sed -e 's/^[0]*//'

Source (output of mmls.exe):

GUID Partition Table (EFI)
Offset Sector: 0
Units are in 512-byte sectors

Slot            Start        End          Length       Description
000:  Meta      0000000000   0000000000   0000000001   Safety Table
001:  -------   0000000000   0000002047   0000002048   Unallocated
002:  Meta      0000000001   0000000001   0000000001   GPT Header
003:  Meta      0000000002   0000000033   0000000032   Partition Table
004:  000       0000002048   0000616447   0000614400   Basic data partition
005:  001       0000616448   0001665023   0001048576   EFI system partition
006:  002       0001665024   0001927167   0000262144   Microsoft reserved partition
007:  003       0001927168   0300832767   0298905600   Basic data partition
008:  004       0300832768   0500117503   0199284736   Basic data partition
009:  -------   0500117504   0500118191   0000000688   Unallocated

I would like to extract only the start offset without leading zeroes. With awk 'NR==x' I choose the line number. The columns are separated with 2-3 spaces.

Thanks in advance!

Upvotes: 1

Views: 76

Answers (1)

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58491

Following selects the first number as per your example

Select-String -Pattern '\d{10}' -Path mmls.txt | 
    ForEach-Object { $PSItem.Matches[0].Value -replace '^0+', '' } | 
    Where-Object { $PSItem -ne '' } | 
    Select -First 1

or a bit more concise

sls '\d{10}' mmls.txt | 
   %{$_.Matches[0].Value -replace '^0+', ''} | 
   ? {$_ -ne ''} | 
   select -f 1

alternative

gc mmls.txt|%{($_ -split '\s+')[2]}|?{$_.Length -eq 10}|%{$_ -replace '^0+'}|?{$_ -ne ''}|select -first 1

Upvotes: 2

Related Questions