我有一個包含大量日志訊息的文本檔案。我想提取兩個字串模式之間的訊息。我希望提取的訊息在文本檔案中顯示。
我嘗試了以下方法。它可以作業,但不支持 Get-Content 的 -Wait 和 -Tail 選項。此外,提取的結果顯示在一行中,但與文本檔案不同。歡迎輸入:-)
示例代碼
function GetTextBetweenTwoStrings($startPattern, $endPattern, $filePath){
# Get content from the input file
$fileContent = Get-Content $filePath
# Regular expression (Regex) of the given start and end patterns
$pattern = "$startPattern(.*?)$endPattern"
# Perform the Regex opperation
$result = [regex]::Match($fileContent,$pattern).Value
# Finally return the result to the caller
return $result
}
# Clear the screen
Clear-Host
$input = "THE-LOG-FILE.log"
$startPattern = 'START-OF-PATTERN'
$endPattern = 'END-OF-PATTERN'
# Call the function
GetTextBetweenTwoStrings -startPattern $startPattern -endPattern $endPattern -filePath $input
根據 Theo 的回答改進了腳本。 以下幾點需要改進:
- 盡管我在腳本中調整了緩沖區大小,但輸出的開頭和結尾還是以某種方式被修剪了。
- 如何將每個匹配的結果包裝成 START 和 END 字串?
- 我仍然無法弄清楚如何使用
-Waitand-Tail選項
更新的腳本
# Clear the screen
Clear-Host
# Adjust the buffer size of the window
$bw = 10000
$bh = 300000
if ($host.name -eq 'ConsoleHost') # or -notmatch 'ISE'
{
[console]::bufferwidth = $bw
[console]::bufferheight = $bh
}
else
{
$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = $bh
$newsize.width = $bw
$pswindow.buffersize = $newsize
}
function Get-TextBetweenTwoStrings ([string]$startPattern, [string]$endPattern, [string]$filePath){
# Get content from the input file
$fileContent = Get-Content -Path $filePath -Raw
# Regular expression (Regex) of the given start and end patterns
$pattern = '(?is){0}(.*?){1}' -f [regex]::Escape($startPattern), [regex]::Escape($endPattern)
# Perform the Regex operation and output
[regex]::Match($fileContent,$pattern).Groups[1].Value
}
# Input file path
$inputFile = "THE-LOG-FILE.log"
# The patterns
$startPattern = 'START-OF-PATTERN'
$endPattern = 'END-OF-PATTERN'
Get-TextBetweenTwoStrings -startPattern $startPattern -endPattern $endPattern -filePath $inputFile
uj5u.com熱心網友回復:
如果要在讀取行時處理行,則需要在管道中執行呼叫的流式處理,例如 with 。
Get-ContentForEach-Object- 如果您正在使用,這是必須的
Get-Content -Wait,因為這樣的呼叫不會自行終止(它會無限期地等待將新行添加到檔案中),但在管道內,它的輸出可以按原樣處理收到,甚至在命令終止之前。
- 如果您正在使用,這是必須的
您正在嘗試跨多行
Get-Content進行匹配,只有在您使用-Raw開關時才能使用輸出 - 默認情況下,逐行Get-Content讀取其輸入檔案。- 但是,
-Raw與-Wait. - 因此,您必須堅持逐行處理,這要求您分別匹配開始和結束模式,并跟蹤處理這兩個模式之間的行的時間。
- 但是,
這是一個概念證明,但請注意以下幾點:
-Tail 100是硬編碼的 - 根據需要調整或使其成為另一個引數。的使用
-Wait意味著該功能將無限期地運行 - 等待添加新行$filePath- 因此您需要使用Ctrl-C來停止它。雖然您可以在管道中使用
Get-TextBetweenTwoStrings呼叫本身進行逐個物件處理,但在使用 終止時將其結果分配給變數 ($result = ...) 將不起作用Ctrl-C,因為這種終止方法也會中止賦值操作。為了解決這個限制,下面的函式被定義為一個高級函式,它自動啟用對common
-OutVariable引數的支持,即使在終止的情況下也會填充Ctrl-C; 然后,您的示例呼叫如下所示(如 Theo 所述,不要將自動$input變數用作自定義變數):# Look for blocks of interest in the input file, indefinitely, # and output them as they're being found. # After termination with Ctrl-C, $result will also contain the blocks # found, if any. Get-TextBetweenTwoStrings -OutVariable result -startPattern $startPattern -endPattern $endPattern -filePath $inputFile
根據您的反饋,您希望行塊包含開始和結束模式匹配的完整行,因此下面的正則運算式包含在
.*和引數中的單詞模式有點模棱兩可,因為它表明它們本身就是正則運算式,因此可以按原樣使用或按原樣嵌入在運算子 RHS 上的更大正則運算式中。 但是,在下面的解決方案中,我假設它們被視為文字字串,這就是為什么它們用;轉義的原因。如果這些引數本身確實是正則運算式,則只需省略這些呼叫;IE:
$startPattern$endPattern-match[regex]::Escape()$startRegex = '.*' $startPattern '.*' $endRegex = '.*' $endPattern '.*'The solution assumes there is no overlap between blocks and that, in a given block, the start and end patterns are on separate lines.
Each block found is output as a single, multi-line string, using LF (
"`n") as the newline character; if you want a CRLF newline sequences instead, use"`r`n"; for the platform-native newline format (CRLF on Windows, LF on Unix-like platforms), use[Environment]::NewLine.
# Note the use of "-" after "Get", to adhere to PowerShell's
# "<Verb>-<Noun>" naming convention.
function Get-TextBetweenTwoStrings {
# Make the function an advanced one, so that it supports the
# -OutVariable common parameter.
[CmdletBinding()]
param(
$startPattern,
$endPattern,
$filePath
)
# Note: If $startPattern and $endPattern are themselves
# regexes, omit the [regex]::Escape() calls.
$startRegex = '.*' [regex]::Escape($startPattern) '.*'
$endRegex = '.*' [regex]::Escape($endPattern) '.*'
$inBlock = $false
$block = [System.Collections.Generic.List[string]]::new()
Get-Content -Tail 100 -Wait $filePath | ForEach-Object {
if ($inBlock) {
if ($_ -match $endRegex) {
$block.Add($Matches[0])
# Output the block of lines as a single, multi-line string
$block -join "`n"
$inBlock = $false; $block.Clear()
}
else {
$block.Add($_)
}
}
elseif ($_ -match $startRegex) {
$inBlock = $true
$block.Add($Matches[0])
}
}
}
uj5u.com熱心網友回復:
首先,你不應該使用$input自定義變數名,因為這是一個自動變數。
然后,您將檔案作為字串陣列讀取,而您希望讀取的是單個多行字串。對于該附加開關-Raw到 Get-Content 呼叫。
您正在創建的正則運算式不允許在您提供的開始和結束模式中使用 fgor 正則運算式特殊字符,因此我建議[regex]::Escape()在創建正則運算式字串時使用這些模式。
雖然您的正則運算式確實在括號內使用了組捕獲序列,但在獲得您尋求的價值時,您并沒有使用它。
最后,我建議對函式名使用 PowerShell 命名約定(動詞-名詞)
嘗試
function Get-TextBetweenTwoStrings ([string]$startPattern, [string]$endPattern, [string]$filePath){
# Get content from the input file
$fileContent = Get-Content -Path $filePath -Raw
# Regular expression (Regex) of the given start and end patterns
$pattern = '(?is){0}(.*?){1}' -f [regex]::Escape($startPattern), [regex]::Escape($endPattern)
# Perform the Regex operation and output
[regex]::Match($fileContent,$pattern).Groups[1].Value
}
$inputFile = "D:\Test\THE-LOG-FILE.log"
$startPattern = 'START-OF-PATTERN'
$endPattern = 'END-OF-PATTERN'
Get-TextBetweenTwoStrings -startPattern $startPattern -endPattern $endPattern -filePath $inputFile
會導致類似:
blahblah
more lines here
使(?is)正則運算式不區分大小寫,并且還具有點匹配換行符
很高興看到您正在使用我的Get-TextBetweenTwoStrings功能版本,但是我相信您將控制臺中的輸出誤認為是在專用文本編輯器中輸出。在控制臺中,太長的行將被截斷,而在記事本等文本編輯器中,您可以選擇換行或使用水平滾動條。
如果您只是附加
| Set-Content -Path 'X:\wherever\theoutput.txt'
例如,在Get-TextBetweenTwoStrings ..通話中,當您在 Word 或記事本中打開它時,您會發現這些行不會被截斷。
事實上,你可以讓這條線跟隨
notepad 'X:\wherever\theoutput.txt'
讓記事本立即打開該檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444138.html
上一篇:在AD組下搜索用戶成員及其部門
