0

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

8
  • That did not post correctly, sorry. Example one outputs each word on a separate line. I would like it to be First Last on one line then break. Commented Jun 5, 2015 at 12:56
  • Thanks for the edit Jeroen!!! Commented Jun 5, 2015 at 12:57
  • 2
    I doubt that Select-String "First:","Last:" gives you the output, that you describe, as the strings in your output neither contain "First:" nor "Last:". Commented Jun 5, 2015 at 13:21
  • 1
    Please provide sample input that you expect to turn into your sample output. Commented Jun 5, 2015 at 14:15
  • 2
    Please edit your question with a few lines of the data. You can genericize any user specific information if you are worried about privacy but seeing what you want to parse will help immensely with deciding how to actually parse it. Commented Jun 5, 2015 at 15:06

1 Answer 1

1

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

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:". 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
Sign up to request clarification or add additional context in comments.

1 Comment

v2 added to support middlenames and etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.