【发布时间】:2021-06-22 12:03:20
【问题描述】:
其实我想从日志文件中提取值并保存到 csv 文件中
这是日志文件
开始时间:1990 年 5 月 17 日星期二 10:38:27
来源 : A:\Live
目标:X:\复制\目标\
文件:*.
选项:. /COPY:DAT /R:1000000 /W:3
新目录:3 W:\Live
新文件:100.0 m randomfile.100M.xyz
0.0%
列表项
0.0%
0.0%
,,,,
,,,,
100%
Total Remaining copied Extracted
光盘:1 1 0 0
文件:3 3 0 0
名称 : 300.00 m 300.00 m 0 0
时间:0:00:47 0:00:46 0:00:00 0:00:00
速度:1224242 字节/秒。 速度:3233.5920 兆字节/分钟。
结束时间:1990 年 5 月 11:39:15 星期二
我想得到以下格式
开始,来源,目的地,速度,结束
1990 年 5 月 17 日星期二 10:38:27,A:\Live,X:\Copy\Dest,1224242 字节/秒,3233.5920 兆字节/分钟,5 月 11 日星期二 11:39:15
但我得到如下输出:
开始","来源","目的地","结束","速度"
,"",,"Wed Mar 17 13","W"
"c",".",". /COPY",,"\Test"
,,,,
,,,,
,,,,
,,,,
,,,
"1 1 0 0 0 0","3 3 0 0 0 0"
"300.00 m 300.00 m 0 0 0 0","0","6703735 Bytes/sec.","383.590 MB/min.","tue May 11 39"
计划
Param($textlogfile = "Path")
$collection=@()
###trim the white spaces lines from the structured text###
$content = (gc $textlogfile ) | ? {$_.trim() -ne "" }
for($i=0;$i -lt $content.length;$i=$i+5)
{
$Started = $content[$i]
$started = $Started -split ":" |Select-Object -Index 1 |ForEach-Object Trim
$Source = $content[$i+1]
$Source = $Source -split ":" |Select-Object -Index 1 |ForEach-Object Trim
$Dest = $content[$i+2]
$Dest = $Dest -split ":" |Select-Object -Index 1 |ForEach-Object Trim
$Ended = $content[$i+3]
$Ended = $Ended -split ":" |Select-Object -Index 1 |ForEach-Object Trim
$Speed = $content[$i+4]
$Speed = $Speed -split ":" |Select-Object -Index 1 |ForEach-Object Trim
$coll = "" | select Started,Source,Dest,Ended,Speed
$coll.Started = $Started
$coll.Source = $Source
$coll.Dest = $Dest
$coll.Ended = $Ended
$coll.Speed = $Speed
$collection +=$coll
}
$collection | export-csv Path -notypeinformation
你能帮我得到想要的操作吗 请帮助我,因为我是使用 powershell 的新手
【问题讨论】:
-
这里的
$content是什么?看来您在$Ended中没有值,并且无法拆分。但是,首先打印 $Ended 的值。请分享Minimal, Verifiable, Reproducible code -
添加检查 $Ended 是否为 -eq $null
-
请正确格式化您的代码
-
是的,我尝试使用 -eq $null 但这不起作用 -@Golden Lion
标签: powershell