0

I'm trying to use powershell to Get-Content from a .txt file, then set-content after filtering the data using an array. I see plenty examples how to do this for 1 word, but not any word in an array. I have searched and tried adopting other solutions here and nothing is working for me. What can I put in my foreach loop to acommplish this? I feel like this will have a very simple answer and I'm overcomplicating it.

#Put account numbers into array.
    $acctArr = "123456789101","121314151617","181920212223","242526272829"

#Use the array to Output a new NB file that only contains the accounts from the array. 
    $fileData = (Get-Content -Path "C:\testfile.txt") | Select-String -Pattern $acctArr -SimpleMatch

foreach($item in $fileData){

    <# Need code here to figure out how to check the $fileData array for strings in $acctArr 
    then if a line of the file has one of the numbers in $acctArr let's do a set-content or Out-file of some kind #>

}

Edit: I tried solutions from this but set-content appears to set the whole file, even lines that do not match what is in my $acctArr

Another Edit: I tried this also. I can write-host the $fileData variable and it outputs to the screen correctly, but if I do a set-content, it doesn't write to the file like I expect:

$fileData = (Get-Content -Path "C:\Folder\testfile.txt") | Select-String -Pattern $acctArr -SimpleMatch
$fileData | Set-Content Output.txt
shadow2020
  • 1,315
  • 1
  • 8
  • 30
  • 2
    your problem is the `Select-String` output - it is NOT string data ... it is _matchinfo_ data. the display system converts that _for display_, but you will need something like `Format-Table` or `Out-File` to make it show up correctly in a plain text file. – Lee_Dailey Jun 20 '19 at 23:09

1 Answers1

1

here is one way to do it. [grin] it builds a regex OR by taking the target words and joining them into a pipe-delimited string. then it runs -match with the input file on the right of the operator and the regex on the left. that gives you all the lines that match any one-or-more of the words in the word list.

$WordSourceFile = 'C:\Temp\Grouping-Words-List_2019-06-12.log'
$WordCount = 3
$TargetFile = 'C:\Temp\Grouping-Strings-List_2019-06-12.log'


$WordsToFind = Get-Random -Count $WordCount -InputObject (
    Get-Content -LiteralPath $WordSourceFile |
    Select-Object -SkipLast 3
    )
$Regex_WTF = $WordsToFind -join '|'

$InLines = Get-Content -LiteralPath $TargetFile
$OutLines = @($InLines -match $Regex_WTF)

'Words to find            = {0}' -f "$WordsToFind"
'Lines in the Target File = {0}' -f $InLines.Count
'Lines in the Output list = {0}' -f $OutLines.Count
'=' * 50
$OutLines

output ...

Words to find            = Tejano Taiko Banjo
Lines in the Target File = 1859
Lines in the Output list = 18
==================================================
Banjo
Banjo Bluegrass Guitar
Banjo Bluegrass Guitar Instrumental
Banjo Bluegrass Guitar Instrumental Railroad
Banjo Celtic Instrumental
Banjo Dupe Instrumental
Banjo Female Guitar
Banjo Guitar Rockabilly Rowdy
Banjo Instrumental
Banjo Instrumental Live
Blues Country Guitar Latin Tejano
Country Guitar Latin Tejano
Drum Dupe Instrumental Japanese Taiko
Drum Instrumental Japanese Taiko
Dupe Guitar Latin Rowdy Tejano
Guitar Latin Spanish Tejano
Guitar Latin Tejano
Guitar Tejano

you can save $Outlines to a CSV or to a plain text file as needed.

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • @shadoe2020 - i don't have your files ... and you did not post a sample. soooooo ... i used what i have. [*grin*] remove the part that reads the words from a file & replace it with your word list - that should work. ///// the point is to [1] build a regex OR list of the target words [2] load the file to be checked into an array [3] use `$Array -match $WordListRegex` to get the lines that match your list [4] save that to a file – Lee_Dailey Jun 20 '19 at 22:49
  • @shadoe2020 - you are most welcome! glad to have helped a bit ... [*grin*] – Lee_Dailey Jun 20 '19 at 23:46