2013-10-30 37 views
0

我有一個文件serviceOut_live.txt本線:如何在同一行上強制輸出字符串?

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

而且我希望將文件serviceOut_live.txt轉換到另一個文件resul.txt這種格式:

1028838 20131030165102 

這是我的代碼:

$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 

但結果是兩行:

1028838 

20131030165102 

而且我想要的結果在一條線:

1028838 20131030165102 

我怎麼能這樣做呢?


非常感謝您的提示。 我認爲我沒有解釋我自己。 這是原始的文件: [GC 1028838K-> 827912K(1305024K),0.0311954秒] 我想要的結果在多行:

\ N =回車

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

我該怎麼做?

回答

1

一種方法是將它轉換爲[字符串]:與當前

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

替換每個行以匹配組的第一空間後和「K」之前發現數字,然後CONCAT它日期

Get-Content $input_path | 
ForEach-Object { ($_ -replace '^\[GC (\d+)K->.*$','$1') +' '+ (get-date -f yyyyMMddHHmmss) } | 
Out-File $output_file 
0

如果你真的喜歡排長隊(我不知道):

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

注意,這也只是得到的F如果找不到匹配項,則匹配該文件並失敗。

相關問題