2016-11-29 112 views
0

我試圖從PowerShell的.txt文件中的多行提取某些值。我目前正在使用多個替換和刪除cmd,但它不能按預期工作,並且有點太複雜。 有沒有更簡單的方法來做到這一點?使用PowerShell從.txt文件中的字符串提取某些值

我的腳本:

$file = Get-Content "C:\RS232_COM2*" 

foreach($line in $file){ 
$result1 = $file.replace(" <<< [NAK]#99","") 
$result2 = $result1.remove(0,3) #this only works for the first line for some reason... 
$result3 = $result2.replace("\(([^\)]+)\)", "") #this should remove the string within paranthesis but doesn't work 

.txt文件:

29 09:10:16.874 (0133563471) <<< [NAK]#99[CAR]0998006798[CAR] 
29 09:10:57.048 (0133603644) <<< [NAK]#99[CAR]0998019022[CAR] 
29 09:59:56.276 (0136542798) <<< [NAK]#99[CAR]0998016987[CAR] 
29 10:05:36.728 (0136883233) <<< [NAK]#99[CAR]0998050310[CAR] 
29 10:55:36.792 (0139883179) <<< [NAK]#99[CAR]099805241D[CAR]0998028452[CAR] 
29 11:32:16.737 (0142083132) <<< [NAK]#99[CAR]0998050289[CAR]0998031483[CAR] 
29 11:34:16.170 (0142202566) <<< [NAK]#99[CAR]0998034787[CAR] 
29 12:01:56.317 (0143862644) <<< [NAK]#99[CAR]0998005147[CAR] 

輸出我期待:

09:10:16.874 [CAR]0998006798[CAR] 
09:10:57.048 [CAR]0998019022[CAR] 
09:59:56.276 [CAR]0998016987[CAR] 
10:05:36.728 [CAR]0998050310[CAR] 
10:55:36.792 [CAR]099805241D[CAR]0998028452[CAR] 
11:32:16.737 [CAR]0998050289[CAR]0998031483[CAR] 
11:34:16.170 [CAR]0998034787[CAR] 
12:01:56.317 [CAR]0998005147[CAR] 
+0

'$ result1 = $ file.replace' - >'$ result1 = $ line.replace' –

回答

1

或更爲簡單:

$Array = @() 
foreach ($line in $file) 
{ 
$Array += $line -replace '^..\s' -replace '\s\(.*\)' -replace '<<<.*#\d+' 
} 
$Array 
1

多個問題。

在循環內部您參考$file而不是$line。在過去的操作,您使用的String.Replace()方法用正則表達式模式 - 這方法不明白 - 使用-replace操盤手:

$file = Get-Content "C:\RS232_COM2*" 

foreach($line in $file){ 
    $line = $line.Replace(" <<< [NAK]#99","") 
    $line = $line.Remove(0,3) 

    # now use the -replace operator and output the result 
    $line -replace "\(([^\)]+)\)","" 
} 

你可以做到這一切在一個正則表達式替換:

$line -replace '\(\d{10}\)\ <<<\s+\[NAK]\#99','' 
+0

非常感謝。易於理解和很好的解釋。 –

1

另一種選擇是隻獲取一條線,你需要一個正則表達式的零件和Concat的他們:

$input_path = 'c:\data\in.txt' 
$output_file = 'c:\data\out.txt' 
$regex = '(\d+(?::\d+)+\.\d+).*?\[NAK]#99(.*)' 
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { [string]::Format("{0} {1}", $_.Groups[1].Value, $_.Groups[2].Value) } > $output_file 

正則表達式是

(\d+(?::\d+)+\.\d+).*?\[NAK]#99(.*) 

參見regex demo

詳細

  • (\d+(?::\d+)+\.\d+) - 第1組:一個或多個數字,隨後爲: 1+序列和一個或多個數字,然後.和再1+數字
  • .*?\[NAK]#99 - 除換行符以外的任何0+字符儘可能少在第一[NAK]#99文字字符序列
  • (.*) - 第2組:行

後,我們得到了所有比賽的其餘部分,與$_.Groups[2].Value級聯的$_.Groups[1].Value取得預期的輸出。

相關問題