0

I am just learning powershell and cant find how to run differents regex with powershell.

$input_path = 'C:\site-download\input.txt'
$output_file = 'C:\site-download\output.txt'
$regex = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex2 = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex3 = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'
$regex... = '(?<month>VPIP<\/span><span class=""right"">\d{2}.\d{1})'

select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches} | % { $_.Value }| 
Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} > $output_file

$regex works good, but how can i add $regex2 and $regex3 ... to outputfile?

Thanks

1 Answer 1

1

You just need a small change to your last section of your pipeline. Instead of using > $output_file just pipe the output of the foreach loop to Out-File cmdlet. So you should be able to have your last line of code look like this:

select-string -Path $input_path -Pattern $regex -AllMatches | 
    % { $_.Matches} | % { $_.Value } |
    Foreach-Object {$_ -replace '</span><span class=""right"">', ' = '} | 
    Out-File $output_file
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot that did the work. I also replace Out-File with Add-Content.
@Elx That works but understand that Add-Content has to convert the output to string before it writes it to the file, where Out-File will just output it to the file. You might see format change in your file depending on what you pipe to each.

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.