Merging two CSV files into one CSV

Welcome Forums General PowerShell Q&A Merging two CSV files into one CSV

Viewing 1 reply thread
  • Author
    • #47163
      Topics: 42
      Replies: 42
      Points: 38
      Rank: Member

      I have two IIS logfiles. I am searching a string name: “externalsystemorderservice” and convertin those IISlogfiles to CSV files.
      $log = Get-ChildItem -Path C:\Temp\Test\0.log
      $col = {@()}.invoke()
      switch -Regex -File $log
      ‘#Fields.*’ {$fields = $_}
      ‘\d+.*externalsystemorderservice.*’ {$col.add($Matches[0])}
      $fields = $fields -replace ‘#Fields:’
      $fields = $fields.trim() -split ‘ ‘
      $col -replace ‘ ‘,’,’ | convertfrom-csv -Header $fields |
      Export-Csv -Path C:\Temp\Test\0.csv -NoTypeInformation

      The same way, I am doing for other logfile as-well.

      Problem is, I have common fields in both CSV files & that needs to be merged & come as third CSV file.
      Common fields: date,time,s-computername,s-ip,time-taken.


    • #47326
      Topics: 5
      Replies: 323
      Points: 486
      Helping Hand
      Rank: Contributor

      Run your switch statement for each log file then export the results. Make the following changes.

      $logs = Get-ChildItem -Path \\path\to\iislogs
      $col = {@()}.invoke()
      foreach ($log in $logs){
      switch -Regex -File $log
      '#Fields.*' {$fields = $_}
      '\d+.*externalsystemorderservice.*' {$col.add($Matches[0])}
Viewing 1 reply thread
  • The topic ‘Merging two CSV files into one CSV’ is closed to new replies.