jeudi 27 août 2020

Powershell multiline pattern matching and picking those lines

I am trying to pick the lines from a log file based on multi-line match. How do we achieve this with Powershell?

LogFile:
Date: 2020-05-06 2:30:33 PM
Name: ABC.txt
{text}
{text}
{text}
Date: 2020-04-08 3:30:33 PM
Name: PQR.txt
{text}

Pattern I am trying to match is -
Date:
Name:

I want to pull all the lines with this pattern. I tried the below line but it doesn't work.
get-content $log_file | select-string -pattern "(?ms)Date:.\nName:." -AllMatches

Output I am looking is -
Date: 2020-05-06 2:30:33 PM
Name: ABC.txt
Date: 2020-04-08 3:30:33 PM
Name: PQR.txt

After this I want to create an array or a tabular format data(preferred) as -
Date|Name
2020-05-06 2:30:33 PM|ABC.txt
2020-04-08 3:30:33 PM|PQR.txt

Thanks for all your support!!!!

Aucun commentaire:

Enregistrer un commentaire