Search multiple files for specific pattern

Welcome Forums General PowerShell Q&A Search multiple files for specific pattern

Viewing 4 reply threads
  • Author
    Posts
    • #198128
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      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?

       

    • #198245
      Participant
      Topics: 5
      Replies: 2411
      Points: 6,193
      Helping Hand
      Rank: Community MVP

      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.

      • This reply was modified 6 months, 3 weeks ago by Olaf.
      • This reply was modified 6 months, 3 weeks ago by Olaf.
      • This reply was modified 6 months, 3 weeks ago by Olaf.
      • This reply was modified 6 months, 3 weeks ago by Olaf.
    • #198323
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      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:-)

       

       

    • #198326
      Participant
      Topics: 1
      Replies: 2
      Points: -3
      Rank: Member

      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

    • #198425
      Participant
      Topics: 5
      Replies: 2411
      Points: 6,193
      Helping Hand
      Rank: Community MVP

      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

      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“.

Viewing 4 reply threads
  • The topic ‘Search multiple files for specific pattern’ is closed to new replies.