Home > OS >  How to read file content based on keywords with power shell?
How to read file content based on keywords with power shell?

Time:12-15

I have a log file with tens of thousands of lines in it, and I need these contents between the keyword A and B. Keywords A and B are unique in the file. How can I get the content via power shell script and save the content to a new file? Any suggestions are appreciated. Thanks.

The input data can look something like this:

A
sprd-spipe: spipe 5-4 not ready to open!
sprd-spipe: spipe 5-4 not ready to open!
systemserver cmd , get para: ( 50)
sprd-spipe: spipe 5-4 not ready to open!
sprd-spipe: spipe 5-6 not ready to open!
sprd-spipe: spipe 5-4 not ready to open!
B

What i tried

cls
$text = gc -raw D:\test\example_out.txt
Write-Host $text
$regex = '(?ms)A:(. ?)B:'
#Output
[regex]::Matches($text,$regex) | 
foreach {$_.groups[1].value } |
Out-File D:\test\example_outs.txt -Encoding utf8
$result = gc -raw D:\test\example_outs.txt
Write-Host $result

And

$test1 = 'A'
$text2 = 'B'
Get-Content D:\test\example_out.txt | Select-String - From $test1 - To $test2 | Set-Content D:\test\outfile.txt

But cannot get correct result.

Update1

Using (?ms)A\n(. ?)B\n? , it works.

A and B in the log should be a sentence, and now, I change the script to

$text = gc -raw D:\test\dumpstate-2021-12-08-21-22-53.txt
$A= 'DUMP OF SERVICE batterystats:'
$B = 'Per-PID Stats:'
#Write-Host $text
$regex = '(?ms)$A\n(. ?)$B\n?'
#Output
[regex]::Matches($text,$regex) | 
foreach {$_.groups[1].value } |
Out-File D:\test\example_outs.txt -Encoding utf8
$result = gc -raw D:\test\example_outs.txt
Write-Host $result

It did not return results

DUMP OF SERVICE batterystats:
sprd-spipe: spipe 5-4 not ready to open!
sprd-spipe: spipe 5-4 not ready to open!
systemserver cmd , get para: ( 50)
sprd-spipe: spipe 5-4 not ready to open!
sprd-spipe: spipe 5-6 not ready to open!
sprd-spipe: spipe 5-4 not ready to open!
Per-PID Stats:

CodePudding user response:

Based on your updated question, mostly everything is fine.

That being siad, if you want to expand variable within a string, you need to use a double-quoted string instead of single-quoted one.

Thus, the regex statement here:

$A= 'DUMP OF SERVICE batterystats:'
$B = 'Per-PID Stats:'
#Write-Host $text
$regex = '(?ms)$A\n(. ?)$B\n?'

Need to be changed to :

"(?ms)$A\n(. ?)$B\n?"

Note:

You should take a look at some .net regex testing online tool. They are super practical to finetune your regex statement until you get what you want. That wouldn't have fixed your quoting issue (single vs double-quote issue) but would have help you find the proper regex statement and see the results live as you experiment.

References:

About Quoting Rules

Single-quoted strings

A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed

Double-quoted strings

A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.

Online regex tester (https://regexstorm.net/tester)

  • Related