正则表达式 – Powershell在String中搜索并提取字符串的特定值
发布时间:2020-12-14 05:36:05 所属栏目:百科 来源:网络整理
导读:我有一个包含许多行的大文件.例如: ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this 从每一行我想提取以下信息: ts =,system = something =,但是=之后的值总是会改变. 我试过这个,但无法让它起作用: $fou
我有一个包含许多行的大文件.例如:
ts=2019-01-16 network=1.1.1.1 system=irgendwas pid=100 bugReq=dasf something=else maybe=this 从每一行我想提取以下信息: ts =,system =& something =,但是=之后的值总是会改变. 我试过这个,但无法让它起作用: $found = $string -match '.*system="(d+)".*' if ($found) { $system= $matches[1]} 解决方法
这是另一个解决方案. [grin]它使用ConvertFrom-StringData cmdlet将输入解析为对象.然后它创建一个只有想要的道具的[PSCustomObject].最后,它将每个对象发送到$Results集合.
虽然最终自定义对象的构造使得以下信息在这种情况下不重要,但重要的是要知道ConvertFrom-StringData cmdlet的输出是标准哈希表.这意味着对象的顺序几乎肯定不是原始顺序.不要指望事物按照它们在源中出现的顺序排列. [edit =添加了一个带有嵌入空格的新数据行和一个更新的替换模式来处理它.] # 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 屏幕输出…… 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 它是一个简单对象的合适集合,因此它将整齐地导出CSV. [笑容] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |