-2

更新

我有一个包含一些参考的 1000 行日志文件。

Time Reference Date of start Date of end
12:00 AT001 13 November 2011 15 November 2011
13:00 AT038 15 December 2012 17 December 2012
14:00 AT076 17 January 2013 19 January 2013

$ref1 = AT038

基本上,我想解析日志文件并为 $ref1 输出(逐行),例如:

Time : 13h
Reference : AT038
Date of start : 15 December 2012
Date of end : 17 December 2012

提前致谢

4

3 回答 3

1

尝试:

$ref1 = "AT038"
$csv = Import-Csv .\myfile.txt -Delimiter ' '#Import file as CSV with space as delimiter
$csv | ? { $_.reference -EQ $ref1 } | FL #Piping each line of CSV to where-object cmdlet,  filtering only line where value of column reference is equal to $ref1 variable value. Piping the result of the filtering to file-list to have output as requested in OP.

在必要条件后添加的代码在 OP 中进行了更改:

$ref1 = "AT038"
$txt = gc .\myfile.txt
$txt2 = $txt | % { $b = $_ -split ' '; "$($b[0]) $($b[1]) $($b[2])_$($b[3])_$($b[4]) $($b[5])_$($b[6])_$($b[7])" }
$csv = convertfrom-csv -InputObject $txt2 -Delimiter ' ' 
$csv | ? { $_.reference -EQ $ref1 } | FL 
于 2012-12-07T09:13:28.883 回答
0

这个怎么样:

Get-Content SourceFileName.txt | 
% { ($_ -Replace '(\d{2}):\d{2} (\w{2}\d{3})', 'Time : $1h|Reference : $2').Split('|')} |
Out-File TargetFileName.txt
于 2012-12-07T09:30:56.523 回答
0

这是我的修订版:

$regex = '(\d{2}):\d{2} (\w{2}\d{3}) (\d{2} \b\w+\b \d{4}) (\d{2} \b\w+\b \d{4})'
$replace = 'Time : $1h|Reference : $2|Date of start : $3|Date of end : $4'
Get-Content SourceFileName.txt | 
% { ($_ -Replace $regex, $replace).Split('|')} |
Out-File TargetFileName.txt
于 2012-12-07T11:58:50.523 回答