regex - Powershell 在字符串中搜索并提取字符串的特定值

标签 regex string powershell

我有一个包含很多行的大文件。例如:

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] 只有想要的 Prop 。最后,它将每个对象发送到 $Results 集合。

虽然最终自定义对象的构造在这种情况下使以下信息不重要,但重要的是要知道 ConvertFrom-StringData cmdlet 的输出是一个标准哈希表。这意味着对象的顺序几乎肯定不会是原来的顺序。 不要期望事情按照它们在源代码中出现的顺序

[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

屏幕输出...

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

它是简单对象的适当集合,因此它可以非常巧妙地Export-CSV。 [咧嘴一笑]

关于regex - Powershell 在字符串中搜索并提取字符串的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54392371/

相关文章:

javascript - 正则表达式阻止连续 4 次以上包含相同数字的电话号码?

python - 在python中的另一个较长列表中搜索列表项

Powershell Invoke-Command 导致不同的结果

powershell - 如何设置使用Powershell可以随时访问的环境变量?

winforms - 从另一个运行空间向表单添加元素

javascript - 以某个字符串开头的单词的正则表达式 (javascript)

javascript - 检查字符串的末尾是否存在有效的正则表达式并返回正则表达式 trim 后的字符串-

java - 如何在Java中将包含多组字符串的字符串拆分为集合集合

Python, Unicode解码错误 : 'ascii' codec can't decode byte 0xc2 in position 1718: ordinal not in range(128)

java - 检查字符串是否为 null 或空格