我在PowerShell中寻找一个等效的命令:
的Linux / MACOS:
cat mmls.txt | awk '{print $3}' | egrep -o '[0,9]{10}' | awk 'NR==1' | sed -e 's/^[0]*//'
来源(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
我想只提取没有前导零的起始偏移量。使用awk'NR == x',我选择行号。列用2-3个空格分隔。
提前致谢!
答案 0 :(得分:2)
以下按照您的示例选择第一个数字
Select-String -Pattern '\d{10}' -Path mmls.txt |
ForEach-Object { $PSItem.Matches[0].Value -replace '^0+', '' } |
Where-Object { $PSItem -ne '' } |
Select -First 1
或更简洁
sls '\d{10}' mmls.txt |
%{$_.Matches[0].Value -replace '^0+', ''} |
? {$_ -ne ''} |
select -f 1
替代
gc mmls.txt|%{($_ -split '\s+')[2]}|?{$_.Length -eq 10}|%{$_ -replace '^0+'}|?{$_ -ne ''}|select -first 1