0

I have a file serviceOut_live.txt with this lines:

[GC 1028838K->827912K(1305024K), 0.0311954 secs]

And I want to convert the file serviceOut_live.txt into another file resul.txt with this format:

1028838 20131030165102

This is my code:

$input_path = 'c:\a\a\serviceOut_live.txt'

$output_file = 'c:\a\a\resul.txt'

$regex = '\[GC (\d{0,9})K->(\d{0,9})'

echo $(select-string -Path $input_path -Pattern $regex -AllMatches | % {
  $_.matches[0].groups[1].value , $(get-date -format "yyyyMMddHHmmss")
} ) > $output_file

But the result is in two lines:

1028838

20131030165102

And I want the result in one line:

1028838 20131030165102

How could I do that?


Thank you very much for the tips. I think that I have not explained myself. This is the original file: [GC 1028838K->827912K(1305024K), 0.0311954 secs] And I want the result in multiple lines:

\n = carriage return

1028838 20131030165102\n 1028822 20131030165103\n 1029999 20131030165107\n

How could I do that?

4

3 回答 3

2

将每一行替换为在第一个空格之后和“K”之前找到的匹配数字组,然后将其与当前日期连接

Get-Content $input_path | 
ForEach-Object { ($_ -replace '^\[GC (\d+)K->.*$','$1') +' '+ (get-date -f yyyyMMddHHmmss) } | 
Out-File $output_file
于 2013-10-30T16:54:42.723 回答
1

一种方法是将其转换为 [string]:

 echo ([string]$(select-string -Path $input_path -Pattern $regex -AllMatches |
  % { $_.matches[0].groups[1].value , $(get-date -format "yyyyMMddHHmmss") } )) > $output_file
于 2013-10-30T16:51:54.930 回答
0

如果你喜欢很长的线条(我不喜欢):

$(select-string -Path $input_path -Pattern $regex -AllMatches).Matches[0].Groups[1].Value + " " + (get-date -f yyyyMMddHHmmss) > $output_file

请注意,这也只会获取文件中的第一个匹配项,如果找不到匹配项,则会失败。

于 2013-10-30T17:19:25.573 回答