1

I have some TXT files that have two rows with content I want to match and Add to another file:

I'm using this command:

Select-String -path '.\*.txt' -Pattern '((?<=Pattern1 ).*|(?<=Pattern2: ).*)' -AllMatches| ForEach-Object{$_.Matches.Value} | Add-Content C:\List.xls

It works, but on the output List.xls, the two expected matches from each of the input txts are put in two rows, like this:

Match1
Match2

So, when I try to open (import) my List.xls on Libreoffice Calc, it won't let me put Match1 and Match2 in different columns. It forces two rows.

Is there a way I can change the Select-String code to make the matches come out like this:

Match1,Match2

This way, when I open (import) the List.xls file, Openoffice Calc willl be able to separate Match1 and Match2 in two columns.

Thanks a lot.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Chembs
  • 15
  • 3
  • 1
    I'd recommend to use the extension `CSV` instead of `xls` because it's actually missleading this way. You could use a `[PSCustomObject]` to get a proper table as output. – Olaf Jan 24 '21 at 15:14
  • I see, thanks for pointing that out. CSV seems to be the logical way to approach this indeed! – Chembs Jan 24 '21 at 18:13

1 Answers1

1
$myMatches = Select-String -path '.\*.txt' -Pattern '((?<=Pattern1 ).*|(?<=Pattern2: ).*)' -AllMatches |
ForEach-Object {
    $_.Matches.Value
}
($myMatches -join ',') | Add-Content C:\List.csv

-join joins array elements with a specified delimiter (,).

As per Olaf's comment use the csv extension. Using [PSCustomObject] and Export-Csv might be the way to go depending on what else you need to do in PowerShell.

G42
  • 9,791
  • 2
  • 19
  • 34
  • 1
    Hey there, thanks for answering. Your approach worked! But I guess I have a design flaw here. If there are multiple txts in the folder, the script will put them all in the same row and differente columns. My intention is to treat each txt individually and add them individually to the csv file, making lots of rows with two columns. I guess I'll have to use some kind of loop. I have to learn how to do that too, can you point me to a resource on this approach? – Chembs Jan 24 '21 at 18:09
  • @Chembs - [this post](https://stackoverflow.com/questions/18847145/loop-through-files-in-a-directory-using-powershell) is probably a good starting place, although I like how succinct Joey is [here](https://stackoverflow.com/questions/3605522/how-to-iterate-over-files-with-powershell). – G42 Jan 24 '21 at 20:24
  • Basically you'll need a mix of [**`Get-ChildItem`**](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-5.1) and either [**`foreach`**](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-5.1) or [**`Foreach-Object`**](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-5.1) (click links to go to MSDN documentation) – G42 Jan 24 '21 at 20:25