Get Specific Strings from File and Store in Variable

Viewed 45

My sample log looks like :

2022-09-01 23:13:05Z | error | 2022-09-02 02:13:05 - [Task] Id:120 Name:OPT_VIM_1HEAD Exception with index:18 | 18.9251137 | Exception:
ERROR       connection to partner '10.19.101.17:3300' broken
2022-09-01 23:13:25Z | error | 2022-09-02 02:13:25 - [Task] Id:121 Name:OPT_VIM_1ITEM 
ERROR       connection to partner '10.19.101.22:3300' broken
2022-09-01 23:13:25Z | error | 2022-09-02 02:13:25 - [Task] Id:121 Name:OPT_VIM_1ITEM RunId:7 Task execution failed with error: One or more errors occurred., detail:
ERROR       connection to partner '10.19.101.22:3300' broken

I want to extract the job name OPT_VIM_1HEAD or OPT_VIM_1ITEM (its dynamic) and also the timestamp after the "error" pattern : 2022-09-02 02:13:25 or 2022-09-02 02:13:05 in different variables.

I have also written the script as :

$dir = 'C:\ProgramData\AecorsoftDataIntegrator\logs\'
$StartTime = get-date
$fileList = (Get-ChildItem -Path $dir -Filter '2022-09-02.log' | Sort-Object LastWriteTime -Descending | Select-Object -First 1).fullname
$message =  Get-Content $fileList | Where-Object {$_ -like ‘*error*’}
$message
$details = Select-String -LiteralPath $fileList -Pattern 'error' -Context 0,14 | Select-Object -First 1 | Select-Object Path, FileName, Pattern, Linenumber
$details[0]

But not able to retrieve the tokens mentioned above

1 Answers

Use regex processing via the -match operator to extract the tokens of interest from each line:

# Sample lines from the log file.
$logLines = @'
2022-09-01 23:13:05Z | error | 2022-09-02 02:13:05 - [Task] Id:120 Name:OPT_VIM_1HEAD Exception with index:18 | 18.9251137 | Exception:
ERROR       connection to partner '10.19.101.17:3300' broken
2022-09-01 23:13:25Z | error | 2022-09-02 02:13:25 - [Task] Id:121 Name:OPT_VIM_1ITEM 
ERROR       connection to partner '10.19.101.22:3300' broken
2022-09-01 23:13:25Z | error | 2022-09-02 02:13:25 - [Task] Id:121 Name:OPT_VIM_1ITEM RunId:7 Task execution failed with error: One or more errors occurred., detail:
ERROR       connection to partner '10.19.101.22:3300' broken
'@ -split '\r?\n'

# Process each line...
$logLines | ForEach-Object {

  # ... by matching it ($_) against a regex with capture groups - (...) -
  #     using the -match operator.
  if ($_ -match '\| (\d{4}-.+?) - \[.+? Name:(\w+)') {

    # The line matched.
    # Capture groups 1 and 2 in the automatic $Matches variable contain
    # the tokens of interest; assign them to variables.
    $timestamp = $Matches.1
    $jobName = $Matches.2

    # Sample output, as an object
    [PSCustomObject] @{
      JobName = $jobName
      Timestamp = $timestamp
    }
  }

}

Output:

JobName       Timestamp
-------       ---------
OPT_VIM_1HEAD 2022-09-02 02:13:05
OPT_VIM_1ITEM 2022-09-02 02:13:25
OPT_VIM_1ITEM 2022-09-02 02:13:25

For an explanation of the regex and the ability to experiment with it, see this regex101.com page.

Related