Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

Extract info from csv based on specific pattern with PowerShell

Hi,


I have an csv file with "Date, Body" column.

In body column I have this pattern for value:

Report Id: ce444534-18de-421e-a9b3-8ea19cb74960
This email was automatically generated by the Generate Incident Report action.
Message Id: <277339844.68.1582484442633@domain.smtp.com>
Sender: user@domain
Subject: Need info
Recipients: user02@domain
To: user02@domain
To: user03@domain
Severity: Low
Override: No
False Positive: No
Rule Hit: TEST


How can I extract just these informations to csv file with PowerShell?:

Sender: user@domain
Subject: Need info
Recipients: user02@domain
To: user02@domain
To: user03@domain

Thanks
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Post your CSV as attachment.

Avatar of SAM2009

ASKER

Here attachedBody.csv
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

OK, it works also with your file.


Avatar of SAM2009

ASKER

Humm good but ideally it's to be able to export to csv file for processing:-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Nice  :  )


Add one parameter for Export-Csv to be better radable by Excel:

} | Export-Csv -NoTypeInformation -Path $outFile -Delimiter ';'

Open in new window

Avatar of SAM2009

ASKER

Wow, I'm not so familiar like you guys but could you please explain what is in bold here below:      

Switch -RegEx ($_.Body -split '\r?\n') {

            '^(?<Key>Sender|Subject):\s*(?<Value>.*?)\s*$' {

                  $out.($Matches['Key']) = $Matches['Value']}
Avatar of oBdA
oBdA

$_.Body -split '\r?\n'
This is a RegEx will split the body at either CR (\r) (the ? allows it to not be there) and LF/NewLine (\n).
'^(?<Key>Sender|Subject):\s*(?<Value>.*?)\s*$'
This matches any line starting with either Sender or Subject. The (?<Name>) is a "named group" that, if matched, can be addressed by its name in $Matches, instead of having to use the numeric index.
$out.($Matches['Key']) = $Matches['Value']}
This sets the output object's properties. If "Subject" was matched in the line, then the property Subject (its name is now in $Matches['Key']) will be set to whatever followed after the ':' (which is in $Matches['Value'])

Welcome to Regular-Expressions.info
https://www.regular-expressions.info/
Avatar of SAM2009

ASKER

Thanks a lot guys for your help!:-)