Link to home
Start Free TrialLog in
Avatar of wingkchan
wingkchan

asked on

parse a text file for wanted status

Hi Experts,

I am trying to find a way to parse a text file and extract the information I want... but not sure as how to do it well.  I have a text file that would tell the status of some processes running on the computer, and I want to write a script to analyze it, so it can extract the process name and the status (2nd & 4th column) :  (pls see code section)

so far, I have come up with:

@Echo Off
For /f "tokens=2,4 delims= " %%a in (D:\file.txt) Do (
If "%%a"=="adminprogram" Echo %%a %%b > result.txt
If "%%a"=="guestprogramr" Echo %%a %%b >>result.txt
If "%%a"=="systemr" Echo %%a %%b >>result.txt
)

What I wanted to do above is to further extract the 2nd & 4th column information to a text file called "result.txt".  Then I will use a third party program to monitor the "result.txt"... when it finds "result.txt" contains the word "stopped" or "stopping", it'll raise an alert.

But the code above doesn't work, as the spaces in 1st column would offset the token I have in the loop... can anyone help with a better way to extract the information?  or how to fix up this batch file? Thank you very much.
# Example Text file for analyze
--------------------------------------
[1]Admin program                     adminprogram               0005      Running
[2]Guest program                      guestprogram               2528      Running
[3]System                                   system                         8451      Running

Open in new window

Avatar of AmazingTech
AmazingTech

Looks like you're wanting to monitor a service.

Perhaps VBS is a better total solution for you.

Check out:

https://www.experts-exchange.com/questions/24124499/Need-a-script-that-send-a-mail-each-time-a-user-stops-or-disables-a-service.html
Avatar of wingkchan

ASKER

Thanks AmazingTech for your reference.  I think I can use that to monitor some other problems that I am facing.

But, my situation is a little different.  The processes that I want to check are not shown in Window's Services nor Task Manager.  I called them "sub processes"  that belongs an application.  To see whether these sub-processes are running, I will need to type a show status command on command prompt...  That's why I am trying to dump a output of the screen to a plain text, analyze the text file for processes that stopped, and with a 3rd party tool, to send out email alerts.  

Do you have any ideas that I can do this in a smarter way?  Actually I have been hunting around for text parser scripts, hoping to find some useful stuff... but not much luck so far.
ASKER CERTIFIED SOLUTION
Avatar of t0t0
t0t0
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Give AutoIt a look. www.autoitscript.com

It's a useful tool to do things like you need.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks sda100 for the script... but in my case, I want to check some processes that doesn't show on Task Managers... this .wsf file sorts the output of services well... however, it can't show the processes of the application that I need to monitor.  but thank you!
Does the check_process give you what you want?  Even though you might have to fiddle with the output like I did above?

http://www.nagiosexchange.org/cgi-bin/page.cgi?g=Detailed%2F2924.html;d=1

In your original output, can we assume that there will only be 1 space between words in the process description, and at least 2 spaces as a delimeter between columns?  If so, the following solution might work (as an example):

Steve :)

@echo off
SetLocal EnableDelayedExpansion
 
set inputfile="test.txt"
 
for /f "tokens=*" %%i in ('type %inputfile%') do (
    echo %%i|findstr /B "[">nul
 
	if not errorlevel 1 (
		for /f "tokens=2* delims=]" %%j in ("%%i") do (
			set myText=%%j
			set myText=!myText:  =#!
 
			for /f "tokens=1,2,3,4 delims=#" %%k in ("!myText!") do (
				set Name=%%k
				set Desc=%%l
				set id=%%m
				set Status=%%n
				
				echo !Name! : !Desc! : !id! : !Status!
			)
		)
	)
)

Open in new window

What's wrong with my code ??? Does it not work ???



@echo Off
setlocal enabledelayedexpansion

(for /f "tokens=1-5 delims= " %%a in (d:\file.txt) do (
   if "%%e"=="" (
      if /i "%%b"=="system" echo %%b %%d
   ) else (
      if /i "%%c"=="adminprogram" echo %%c %%e
      if /i "%%c"=="guestprogram" echo %%c %%e
   )
))>result.txt
Hi t0t0:

Your code assumes a fixed number of 'words' per line.  As I understand, the 1st column is a description and could be any number of words.  Add this line to the text file, and retry your code:

        [4]Another System Program                   system                         8942      Running

That's why I made the assumption there would be more than one space between fields.

Steve
I assume the following is true:

1) the right-most column will ALWAYS only contain ONE word: the status of the process

2) the 2nd column from the right will ALWAYS contain the process ID

3) the 3rd column from the right will ALWAYS contain ONE word: the name of the owner of the process

4) the column on the left will ALWAYS be prefixed with '[x]' - where x is any integer

5) every thing else immediately following the prefix and the 3rd column from the right is the process name and may comprise of a variable number of lexemes

Please try this batch file.



@echo Off
:Main
   setlocal enabledelayedexpansion
   for /f "tokens=* skip=2" %%a in (file.txt) do call :ProcessLine %%a
exit /b


:ProcessLine
   set Count=0
   
   :Loop
      set /a Count+=1
      set Lex%Count%=%1
      shift
   if not "%1"=="" goto :Loop

   set Status=!Lex%Count%!
   set /a Count-=2
   set Process=!Lex%Count%!
   echo %Process% %Status%>>result.txt
exit /b
wingkchan

Was my previous reply helpful?
Sorry for the late reply... I got carried away with other works that dropped in.  In the meanwhile, A teamate with better scripting experience is helping me to write vbscript files to monitor the process.  But I will still try to use the recommendations above to test them out when I have  more free time.  Thank you and truly appreciate the input.  Will let you know the outcome soon.  Thanks!
AmazingTech
I didn't know any other way of contacting you. I hope you don't mind me sliding in like this.

I wonder if you could help me with a problem I have in ID: 24321396 regarding error messages. Thanks.
sorry guys, I totally forgot about this until now... thanks for all the contributions.  I didn't finish this task at last, as the programers found a way to do what they wanted. thanks