1

I am trying to extract all text matching a particular pattern from files in the directory using PowerShell. However, for some reason it seems to loop indefinitely (output is an endlessly repeating set of the same results and the script never finishes). Please, can anyone give me a tip on what the problem is?

$input_path = 'C:\Users\*.txt'
$output_file = 'C:\Users\*.txt'
$regex = '[A-Za-z]+?_V[A-Z][A-Z0]?[A-Z]? [A-Za-z]+?_R[A-Z][A-Z]?V?'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
Daria Da
  • 19
  • 2
  • 5
    Try to move the output file in an other directory. As an aside, using a lazy quantifier here `[A-Za-z]+?_` is useless (`[A-Za-z]` doesn't contain the `_` character). – Casimir et Hippolyte May 15 '18 at 13:41
  • 1
    I don't understand how you're getting this to run in the first place, you can't do `> *` unless there's only one file in the directory. – Jacob Colvin May 15 '18 at 14:07

1 Answers1

1

As pointed out in the comments you should probably save the output to a different folder or capture the files (not just the path) into a variable. Something like this:

$input_files = Get-Childitem 'C:\Users\*.txt'
$output_file = 'C:\Users\output.txt'
$regex = '[A-Za-z]+?_V[A-Z][A-Z0]?[A-Z]? [A-Za-z]+?_R[A-Z][A-Z]?V?'
($input_files | Select-String -Pattern $regex -AllMatches).Matches.Value > $output_file
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56