简体   繁体   English

将日志文件导出到 Powershell 中的新 CSV 文件

[英]Export Log file to new CSV file in Powershell

I feel like I'm going about this the wrong way, but I've gotten myself half way there... I have a log file called DataSet.log that is formatted as:我觉得我走错了路,但我已经走到了一半……我有一个名为 DataSet.log 的日志文件,其格式为:

First Dataset;24226382;2020-10-01 00:00;Second.Data.Set 1.0;0;Third.DataSet 1.0;2;Fourth.DataSet 1.0;0;Fifth.Dataset 1.0;0
First Dataset;24421469;2020-10-01 01:00;Second.Data.Set 1.0;0;Third.DataSet 1.0;4;Fourth.DataSet 1.0;0;Fifth.Dataset 1.0;0
First Dataset;24667838;2020-10-01 02:00;Second.Data.Set 1.0;0;Third.DataSet 1.0;6;Fourth.DataSet 1.0;0;Fifth.Dataset 1.0;0
First Dataset;24667839;2020-10-01 02:00;Second.Data.Set 1.0;0;Third.DataSet 1.0;1;Fourth.DataSet 1.0;0;Fifth.Dataset 1.0;0

I'm trying to convert this to a new CSV file and get it to display only the third and seventh columns.我正在尝试将其转换为新的 CSV 文件,并使其仅显示第三列和第七列。 I've gotten it to display in PowerShell properly with:我已经让它在 PowerShell 中正确显示:

Import-Csv .\DataSet.log -Header A,B,C,D,E,F,G,H,I,J,K -Delimiter ';' | Format-Table C,G

I've tried to export it as:我试图将其导出为:

$testPush = Import-Csv .\DataSet.log -Header A,B,C,D,E,F,G,H,I,J,K -Delimiter ';' | Format-Table C,G

$testPush | Out-File .\test.csv

And it does create a new csv file, but it's only displaying the third column with a header of "C G."它确实创建了一个新的 csv 文件,但它只显示标题为“CG”的第三列。 Also, that header is in A2, where A1 is blank, and A3 is populated with "- -" before the datetime from the third column populates the remaining rows... What the heck am I doing wrong?此外,该标题在 A2 中,其中 A1 为空白,而 A3 在第三列的日期时间填充剩余行之前填充了“- -”...我到底做错了什么?

Try this:尝试这个:

 $testPush = Import-Csv .\DataSet.log -Header A,B,C,D,E,F,G,H,I,J,K -Delimiter ';' | Select-Object C,G
 $testPush | Export-CSv -Path .\test.csv -NoTypeInformation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM