Extract string from log file

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
dba
Posts: 2
Joined: Fri Dec 08, 2006 3:11 am

Extract string from log file

Post by dba »

Hello,

I'm trying to find a way to extract a value from a log file (F-Secure's HEADER.INI) and to send it back to me as a %Reply%

In that INI file, we find several values and these...
[FSAV_Database_Version]
Version=2006-12-08_03


Actually, I'd like to get 2006-12-08_03 as the Reply

I've tried with the Compare Files test (but I guess there's no way to extract value) and the Text Log test looking for string Version= with Display Found Line...

no luck :-\

Any help would be much appreciated
Thank You


David Basquin
Alternalease
KS-Soft Europe
Posts: 2832
Joined: Tue May 16, 2006 4:41 am
Contact:

Post by KS-Soft Europe »

If you are sure there that entry "Version=" exists in [FSAV_Database_Version] section only, you may use "Text Log" test method with following parameters:
Look for string Version=
Warn of all new events Display:
Item to display : found
Display text from pos #9 to pos #50
http://www.ks-soft.net/hostmon.eng/mfra ... tm#textlog

On the another hand, you may use Schell Script test method:
http://www.ks-soft.net/hostmon.eng/mfra ... m#chkShell
In this case you should create cmd script, that would parse log file and retrieve necessary information.

Regards,
Max
Yoorix
Posts: 177
Joined: Wed Dec 14, 2005 8:28 am

Post by Yoorix »

Hi David.
I think, you may use Shell Script method with following script:
http://www.ks-soft.net/hostmon.eng/tests.htm#chkShell

Start cmd: cmd /c %Script% %Params%
Script :

Code: Select all

@echo off
rem %1 - Filename full path
rem %2 - Section Name
rem %3 - Pattern string

set last_section_name="" 
set string_to_parse=""
set section_start=""
set reply_value=""
set error_str="No match"

rem Step 1: Retrieving raw section line number
for /f "eol=; tokens=*" %%i in ('type %1 ^| find /N "[%2]"') do SET last_section_name=%%i 
IF %last_section_name%=="" (
   SET error_str="Could not locate section %2"
   goto result  
)

rem Step 2: Retrieving real section line number
for /f "eol=; tokens=1* delims=[" %%i in ('echo %last_section_name%') do (
  for /f "eol=; tokens=1* delims=]" %%j in ('echo %%i') do SET section_start=%%j
)

rem Step 3: Retrieving line that mathes pattern
for /f "eol=; tokens=* skip=%section_start%" %%i in ('type %1') do (
   echo %%i | find "%3" > NUL
   IF ERRORLEVEL 1 (
     rem 
   ) ELSE (
     SET string_to_parse=%%i
     goto end
   ) 
)

:end
rem echo %string_to_parse%

rem Step 4: Extracting a value from the string
for /f "eol=; tokens=* delims=" %%i in ('echo %string_to_parse%') do (
  for /f "eol=; tokens=2*" %%j in ('echo %%i') do SET reply_value=%%j
)

:result
IF NOT %reply_value%=="" (
  echo ScriptRes:Ok:%reply_value%
) ELSE (
  echo ScriptRes:Bad:%error_str%
)
Script requires three params:
1. Full file name of log file
2. Section name
3. Pattern string (Version= in your case)

So, into the Params box of Test Properties Window you just should specify appropriate params, like:
C:\temp\HEADER.INI FSAV_Database_Version Version=

Please note: Script may not work with folders with space in their name, like C:\Program Files\Test, etc. In such case just copy log file into the another folder without space in name.

Regards,
Yoorix
dba
Posts: 2
Joined: Fri Dec 08, 2006 3:11 am

Post by dba »

Thank you for your replies; your script looks awesome Yoorix ^^
I'll try both solutions on Monday.

David
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

I am confused.
You want to check log (some file that accumulates information from some application, application adds new data to the file but does not remove/replace old data)?
Or you want to check INI file - file where the same line (e.g. line #5) may store some changeable data?

Regards
Alex
Post Reply