igg*_*gge 3 regex string powershell
我有一个包含许多行的大文件.例如:
ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this
Run Code Online (Sandbox Code Playgroud)
从每一行我想提取以下信息:
ts=,system= & something=,但=总是改变后的价值观.
我试过这个,但无法让它起作用:
$found = $string -match '.*system="(\d+)".*' if ($found) { $system= $matches[1]}
Run Code Online (Sandbox Code Playgroud)
这是另一个解决方案.[ grin ]它使用ConvertFrom-StringDatacmdlet将输入解析为对象.然后它创建一个只有想要的道具的[PSCustomObject].最后,它将每个对象发送到$ Results集合.
虽然最终自定义对象的构造使得以下信息在这种情况下不重要,但重要的是要知道ConvertFrom-StringDatacmdlet 的输出是标准哈希表.这意味着对象的顺序几乎肯定不是原始顺序.不要指望事物按照它们在源中出现的顺序排列.
[edit =添加了一个带有嵌入空格和更新-replace模式的新数据行来处理它.]
# fake reading in a text file
# in real life, use Get-Content
$InStuff = @(
'ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this'
'ts=2019-01-16 network=1.1.1.2 system=PC-001 pid=100 bugReq=dasf something=OtherElse maybe=this'
'ts=2019-01-16 network=1.1.1.66 system=PC-666 pid=100 bugReq=dasf something=ThisELse maybe=this'
'ts=2019-01-16 network=1.1.1.3 system=PC-123 pid=100 bugReq=dasf something=AnotherElse maybe=this'
'ts=2019-01-16 network=1.1.1.4 system=PC-004 Oo-LaLa another value with WhiteSpace id=100 bugReq=dasf something=Else-ish with Whitespace'
)
$Results = foreach ($IS_Item in $InStuff)
{
# this requires that spaces ONLY be found as delimiters
# if you have embedded spaces, some sort of data format adjustment will be required
# now there is a need for handline embedded whitespace
#$IS_Item -replace ' ', [environment]::NewLine |
$IS_Item -replace '(\w{1,}=)', ('{0}{1}' -f [environment]::NewLine, '$1') |
ConvertFrom-StringData |
ForEach-Object {
[PSCustomObject]@{
TS = $_.ts
System = $_.system
Something = $_.something
}
}
}
$Results
Run Code Online (Sandbox Code Playgroud)
屏幕输出......
TS System Something
-- ------ ---------
2019-01-16 irgendwas else
2019-01-16 PC-001 OtherElse
2019-01-16 PC-666 ThisELse
2019-01-16 PC-123 AnotherElse
2019-01-16 PC-004 Oo-LaLa another value with WhiteSpace Else-ish with Whitespace
Run Code Online (Sandbox Code Playgroud)
它是一个简单对象的合适集合,所以它会Export-CSV非常整齐.[ 笑 ]
| 归档时间: |
|
| 查看次数: |
232 次 |
| 最近记录: |