- 1 year ago
How to print all lines from log file after a specific word up to end of paragraph?
To print all lines from a log file after a specific word up to the end of the paragraph, you can use a combination of command-line tools like findstr
or grep
along with a loop in a scripting language like batch or PowerShell. Here's how you can achieve this using both approaches:
Using Windows Command Prompt (Batch Script):
batch@echo off
setlocal enabledelayedexpansion
set "keyword=specific_word"
set "logfile=your_log_file.log"
set "found=false"
for /f "delims=" %%a in (%logfile%) do (
set "line=%%a"
if !found! (
echo !line!
if "!line!" == "" (
set "found=false"
)
)
if "!line!" == "!keyword!" (
set "found=true"
echo !line!
)
)
Replace specific_word
with the word you want to search for and your_log_file.log
with the path to your log file.
Using PowerShell:
powershell$keyword = "specific_word"
$logfile = "C:\path\to\your_log_file.log"
$found = $false
Get-Content $logfile | ForEach-Object {
$line = $_
if ($found) {
Write-Output $line
if ($line -eq "") {
$found = $false
}
}
if ($line -eq $keyword) {
$found = $true
Write-Output $line
}
}
Replace specific_word
with the word you want to search for and C:\path\to\your_log_file.log
with the actual path to your log file.
Both scripts will read the log file line by line, print lines starting from the line containing the specific word, and continue printing until an empty line is encountered (indicating the end of the paragraph). Adjust the script according to your specific requirements and file paths.