1

I have a log file that gives me the following content every day:

Import1:

2019-09-09 19:05:07.568 INFO Stock Record Fetched: alsn_value: 63007409, mtrl: 1, sblc: 1, infc: 2, bacn_value: 9783492058575, locn: 1, chol: 64, shlf: Efm 3.4 SUCH, edatum: 2019-09-09 
2019-09-09 19:05:20.570 INFO Stock Record Fetched: alsn_value: 63012211, mtrl: 1, sblc: 1, infc: 2, bacn_value: 9783125633254, locn: 1, chol: 64, shlf: Ock 2 LANG, edatum: 2019-09-09 

Import 2:

2019-09-09 19:05:22.168 INFO Stock Record Fetched: alsn_value: 63012605, mtrl: 1, sblc: 1, infc: 2, bacn_value: 9783193133076, locn: 1, chol: 64, shlf: Odl 7 COLO A2, edatum: 2019-09-09  

I don't need all the information. I just need:

alsn_value: XXXXXXXXXX,   
bacn_value: XXXXXXX,  
shlf  XXXXX XXX XX ,
edatum: xxxx-xx-xx

I have selected it with the following command, but I do not get the right result:

$select = Select-String -Path "D:\error.log" -Pattern 'alsn_value:' | select line

$asd = $select.Line.Split(' ')[6,7,14,15]

Please suggest how to solve it nicely.

Compo
  • 36,585
  • 5
  • 27
  • 39
densen
  • 11
  • 1

1 Answers1

1

Try something like this:

$input_path = "D:\error.log"

$content = Get-Content $input_path -Tail 1
$alsn = $content -replace '.*alsn_value: ([0-9]+).*', '$1'
$bacn = $content -replace '.*bacn_value: ([0-9]+).*', '$1'
$shlf = $content -replace '.*shlf: (.*?),.*', '$1'
$edatum = $content -replace '.*edatum: ([0-9]+-[0-9]+-[0-9]+).*', '$1'

Write-Output "Read alsn: $alsn"
Write-Output "Read bacn: $bacn"
Write-Output "Read shlf: $shlf"
Write-Output "Read edatum: $edatum"
Read-Host -Prompt "Press enter to exit..."

Note: this will only check the last line of the error log (I assume you don't want to try to parse the whole file). You can tweak the regex yourself if you want to make it a little more robust, I've made some assumptions based on the example you gave.

  • First of all, thank you very much for your answer! If I want to scan the whole file through and then add the output to a file, it's best to work with a Do-While statement, right? – densen Sep 18 '19 at 08:14
  • You can do something like foreach($content in Get-Content .\error.log) { ... }, see https://stackoverflow.com/questions/33511772/read-file-line-by-line-in-powershell – user11645321 Sep 24 '19 at 00:59