Please note that VisualCron support is not actively monitoring this community forum. Please use our contact page for contacting the VisualCron support directly.


CMBI-IT
2020-04-22T10:51:32Z
Hello support,

We know that VC can check a file containing specific keyword/content, may we know if VC can extract that row containing the keyword?

Thanks.
Sponsor
Forum information
jrtwynam
2020-04-23T14:51:22Z
I'm not sure if VisualCron has a task that can do that directly, but you can do it with a PowerShell task. Eg if you have a file named Test.txt on your desktop that contains these lines:


This is Line 1
This is Line 2
This is Line 3
This is Line 4: Here is the line with my keyword
This is Line 5
This is Line 6


Then you could write a simple PowerShell script like this:


$InputFilePath = "C:\Users\<username>\Desktop\Test.txt"
$InputKeyword  = "keyword"

$Line = Select-String -Pattern $InputKeyword -SimpleMatch -Path $InputFilePath 

$Line


That will return output like this:

Desktop\Test.txt:4:This is Line 4: Here is the line with my keyword


If there happens to be more lines containing your keyword, the output will look like this:


Desktop\Test.txt:4:This is Line 4: Here is the line with my keyword
Desktop\Test.txt:7:This is Line 7: Here is another line with my keyword


That output will be stored in the Output variable of your VisualCron PowerShell task, and you can do whatever you want with it from there within VisualCron.
Gary_W
2020-04-23T15:47:38Z
I wrote a powershell script to do just that. I'm no powershell expert, but I'll share it here. I use streams to be memory efficient on large files in case a large log file needed to be searched for a certain error message or date or something. You'll need to know regular expressions as you pass in the file to search, a regex with the pattern to match surrounded by parenthesis, and a flag indicating if you want only the first match or all matches. These parameters are passed by job variables. See the screencap and program header, hopefully that will be all you will need to use it. In the screencaps, {USERVAR(EnvBinDir)} is a global user-defined variable that points to our script directory, sample setting of job variables to get rows from the tnsnames.ora file, and the output.

UserPostedImage UserPostedImage UserPostedImage

############################################################################################################  
#
#  get_line_in_file - Returns line in a file if matched by a regex
#
#  Params:  source_file      - The source file (req'd)
#           regex            - The pattern to match
#           firstMatchOnly   - Y for the first match only
#
#  Output:  The first occurrence of the line or part of line to match if found, NULL if no match
#
#  Notes:   Uses streams so file is not read into memory, unlike Get-Content
#
#           The regex param must be constructed so what you want returned is enclosed
#           in parens, as that is Groups[1]. This is so just a part of the matched line can be 
#           returned if that is what is needed.
#           (.*CONTRACT_TYPE.*) - The entire line will be returned if it contains the text CONTRACT_TYPE.
#           ^ *(.*),.*CONTRACT_TYPE - The text after 0 or more spaces and before the first comma will be
#                                     returned on the first line that contains the text CONTRACT_TYPE.
#
#  Usage in VC: Powershell file: {USERVAR(EnvBinDir)}\get_line_line_in_file.ps1
#               Working directory: <leave blank>
#
#  Author: Gary_W  01/03/2019
#
############################################################################################################   
Param(
  [Parameter(Mandatory=$true)]
  [string] $source_file,             # Source file

  [Parameter(Mandatory=$true)]
  [string] $regex,                   # Regular expression

  [Parameter(Mandatory=$true)]       # Y to return only the first match,
  [string] $firstMatchOnly           # N to return all matches

)

#
#  Loop through the source file
#

$match = $null

$file = New-Object System.IO.StreamReader($source_file, [Text.Encoding]::default, $true, 1MB)  
:main while (!$file.EndOfStream) {
   $line = $file.Readline()
   $match = [regex]::match($line, $regex).Groups[1].Value
   if ( ! [string]::IsNullOrEmpty($match) ) 
   {
     $match  # Print it to STDOUT
     
     if ( $firstMatchOnly.ToUpper() -eq 'Y' )
     {
       break main
     }
   }
}
$file.Close()
$file.Dispose()

##  End get_line_in_file()
Scroll to Top