Search multiple files for specific pattern

My first post in this forum:-)

I have folder with some .txt files. I want to search through the files and find lines where this pattern is part of the line: BDAS. The line3 could look like this:

This is a line number 1 with BDAS-1111

This is a line number 2 with BDAS-1111

This is a line number 3 with BDAS-2222

BDAS-1111 and BDAS-2222 should both be recorded once and the result (filename, BDAS-code) should be written to a .csv file.

I have been able to parse line and filename to a .CSV file, so the content will be this:

This is a line number 1 with BDAS-1111;filename.txt

This is a line number 3 with BDAS-2222;filename.txt

This were done via this:

#Get All objects with ‘BDAS’
Get-ChildItem `
-Path $SourceFolderForSplittedFiles -recurse | `
Select-String -pattern “BDAS” -AllMatches | `
Select-Object -Property @{label=‘JIRA’;expression=({$.Line})}, @{label=‘Objekt’;expression=({$.Filename})}| `
#Select-Object -Property @{label=‘JIRA’;expression=({$.Line})}, @{label=‘Objekt’;expression={$ObjektID = $.Filename; @($ObjektID).GetType() } }| `
Export-CSV “C:\ResultFile2.csv” -Delimiter ‘;’

But I would like the output of my .csv file to be:

BDAS-1111;filename

BDAS-2222;filename

How could I accomplish that?

 

The simplest version should be something like this:

Get-ChildItem -Path $SourceFolderForSplittedFiles -Filter *.txt | 
    ForEach-Object {
        $File = $_
        Select-String -Path $_.FullName -Pattern 'BDAS-\d{4}' | 
            Select-Object -Property @{Name='Match';Expression={$_.Matches.Value}}, @{Name='FileBaseName';Expression={$File.BaseName}}
    }

But please please please do not use backticks. That’s the worst style / habbit you can have for Powershell scripts. Especially when you place them after the pipe symbol … that’s a line continuation charachter anyway.

Hi Olaf

Thanks for your (quick) reply - I’ll try it out as soon as possible. So ‘$_.Matches.Value’ will return ‘BDAS-’ plus whatever comes after that (ie BDAS-1111, BDAS-2222)? That was exactly what I was looking for:-)

I haven’t been able to find any documentation for your -Pattern string ‘\BDAS-\d{4}’? What does ‘\d{4}’ mean?

And I’ll not be using backtics from now on:-)

 

 

It works like a charm:-)

Aha, ‘d{4}’ is a regular expression, and search for all occurence of ‘BDAS-’ followed by a 4-digit number:-) I have changed the script a little to this:

Get-ChildItem -Path $SourceFolderForSplittedFiles -Filter *.txt |
ForEach-Object {
$File = $
Select-String -Path $
.FullName -Pattern ‘PAAS-\d{1,4}’, ‘WK-\d{1,4}’ |
Select-Object -Property @{Name=‘Match’;Expression={$_.Matches.Value}}, @{Name=‘FileBaseName’;Expression={$File.BaseName}}
} |
Export-CSV “C:\ResultFile_FB.csv” -Delimiter ‘;’

I’m searching for occurences of both ‘BDAS-’ and ‘WK-’ followed by a number with 1-4 digits. The result is piped to a CSV-file

[quote quote=198326]It works like a charm:-)

I’m searching for occurences of both ‘BDAS-’ and ‘WK-’ followed by a number with 1-4 digits. The result is piped to a CSV-file[/quote]

I’m glad I could be of help and I’m proud that you figured out by yourself. Great. You can simplyfy your pattern a little bit like this:

Select-String -Path $_.FullName -Pattern '(?:PAAS|WK)-\d{1,4}'

Here you have good source to learn more about regex: https://www.regular-expressions.info

BTW: You can format your code for this forum by marking the code you pasted and click on the code tag button labeld “PRE”.