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
    Posts
    • #47163
      Participant
      Topics: 41
      Replies: 41
      Points: 23
      Rank: Member

      Hi,
      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.

      -Kalyan

    • #47326
      Participant
      Topics: 5
      Replies: 277
      Points: 169
      Helping Hand
      Rank: Participant

      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.