1

Currently I have a script that will search a directory and fine all instances of the word "dummy". It will then output to a CSV the FileName, Path, LineNumber, Line to a file.

This Line contains a very standardized results like:

  • Hi I am a dummy, who are you?
  • Something dummy, blah blah?
  • Lastly dummy, how is your day?

I am trying to find a way to output an additional column in my CSV that contains all characters before the "?" as well as all of the characters after "dummy,".

Resulting lines would be:

  • who are you
  • blah blah
  • how is your day

I tried to use split but it keeps removing additional characters. Is it possible to find the index of "dummy," and "?" and then substring out the middle portion?

Any help would be greatly appreciated.

Code as it stands:

Write-Host "Hello, World!"

# path
$path = 'C:\Users\Documents\4_Testing\fe\*.ts'
# pattern to find dummy
$pattern = "dummy,"

Get-ChildItem -Recurse  -Path $path | Select-String -Pattern $pattern |
Select-Object FileName,Path,LineNumber,Line
,@{name='Function';expression={
    $_.Line.Split("dummy,")
}} |
Export-Csv 'C:\Users\User\Documents\4_Testing\Output1.csv' -NoTypeInformation

Write-Host "Complete"
3
  • What do you mean by "as well as all of the characters after"? "all characters before ?" already contains all the characters after dummy,? Can you give an exact example of what (at least 1 of) the resulting column values should be? Commented Jun 24, 2021 at 18:16
  • I want "Hi I am a dummy, who are you?" to output to: "who are you" Commented Jun 24, 2021 at 18:23
  • 1
    Seems like you're looking for $_.Line.Split(",")[1]. Commented Jun 24, 2021 at 18:26

1 Answer 1

3

Use the -replace operator to replace the whole line with just the part between dummy, and ?:

PS ~> 'Hi I am a dummy, who are you?' -replace '^.*dummy,\s*(.*)\?\s*$', '$1'
who are you

So your calculated property definition should like this:

@{Name = 'Function'; Expression = { $_.Line -replace '^.*dummy,\s*(.*)\?\s*$', '$1' }}

The pattern used above describes:

^         # start of string
 .*       # 0 or more of any character
 dummy,   # the literal substring `dummy,`
 \s*      # 0 or more whitespace characters
 (        # start of capture group
  .*      # 0 or more of any character
 )        # end capture group
 \?       # a literal question mark
 \s*      # 0 or more whitespace characters
$         # end of line/string

If you also want to remove everything after the first ?, change the pattern slightly:

@{Name = 'Function'; Expression = { $_.Line -replace '^.*dummy,\s*(.*?)\?.*$', '$1' }}

Adding the metacharacter ? to .* makes the subexpression lazy, meaning the regex engine tries to match as few characters as possible - meaning we'll only capture up until the first ?.

Sign up to request clarification or add additional context in comments.

2 Comments

This is very close -- I am sorry for not mentioning earlier but is there a way to handle additional text after the "?"? So if the line contains Hi I am a dummy, who are you? Hello? it would still only return: "who are you" Running 'Hi I am a dummy, who are you? Hello?' -replace '^.*dummy,\s*(.*)\?\s*$', '$1' Results in who are you? Hello
@user68288 I've updated the answer to cover this. For future reference, you'll have a better shot at getting useful answers if you include all relevant details in your question up front :-)

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.