简体   繁体   English

具有多种模式的选择字符串,一行输出

[英]select-string with multiple patterns, one line output

I am extracting info from a large text file. 我正在从大型文本文件中提取信息。 When I do this using the code below all my patterns go to separate lines. 当我使用下面的代码执行此操作时,所有模式都移到单独的行。 I would like them to output to one. 我希望他们输出到一个。

get-content c:\dev\test\data.txt | Select-String "First:","Last:" | Add-Content c:\dev\test\output.txt

This currently gives me: 目前,这给了我:

John 约翰
Doe 母鹿
Mary 玛丽
Smith 工匠

I would like: 我想要:

John Doe 约翰·杜
Mary Smith 玛丽·史密斯

You could try something like this: 您可以尝试这样的事情:

$text = @"
First: John
Last: Doe
lasld

First: Mary
dasd
Last: Smith
"@

$text | Select-String '(?s)First:\s+(\w+).*?Last:\s+(\w+)' -AllMatches |
ForEach-Object { $_.Matches } |
ForEach-Object { "$($_.Groups[1].Value) $($_.Groups[2].Value)" }

Output: 输出:

John Doe
Mary Smith

It requires the input as a single multi-line string, so you would need to use $text = Get-Content "c:\\dev\\test\\data.txt" -Raw or $text = (Get-Content "c:\\dev\\test\\data.txt") -join [environment]::NewLine 它要求输入为单个多行字符串,因此您需要使用$text = Get-Content "c:\\dev\\test\\data.txt" -Raw$text = (Get-Content "c:\\dev\\test\\data.txt") -join [environment]::NewLine

Regex101: https://regex101.com/r/bD2oU3/1 Regex101: https ://regex101.com/r/bD2oU3/1

Update: I realized that some people may have middlenames, so as long as the line ends with the name like the samples, you could use the regex below to include more than the first word after "First:" and "Last:". 更新:我意识到有些人可能会有中间名,因此只要该行以示例之类的名称结尾,就可以使用下面的正则表达式在“ First:”和“ Last:”之后包含多个第一个单词。 It also removes trailing whitespace on the line 它还会删除行尾的空格

$text = @"
First: John Lala     
Last: Doe
lasld

First: Mary
dasd
Last: Smith Test
"@

$text | Select-String '(?ms)First:\s+(.*?)\s{0,}$.*?Last:\s+(.*?)\s{0,}$' -AllMatches |
ForEach-Object { $_.Matches } |
ForEach-Object { "$($_.Groups[1].Value) $($_.Groups[2].Value)" }

John Lala Doe
Mary Smith Test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM