Batch Script to count the string

Intel / Intel pentinum
February 12, 2009 at 21:04:08
Specs: Windows XP, 512
Hi Friends,

I want to write a batch script that should read a text file line by line and search for a keyword.
It the keyword is found it should return value 0 else it should return 1.
Like this it should keep on increamenting the value until the end of the file.
The output should be printed in a file.

The sample log is as follows,

Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK
Website is available HTTP/1.1 200 OK

Basically i want to count the number of times the website failed. So, if the website is ok, then it should return value '0' if it is not then value '1'

so atlast i want the total number of times the website failed. Am new to batch script, so i need your help :-)



See More: Batch Script to count the string

Report •


#1
February 14, 2009 at 03:40:35
find /v /c "OK" < myfile


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 15, 2009 at 19:50:00
Hiiii,

Thanks for your response. But actually my task has become a little more complex now, please consider my requirement as follows,

Step 1:

I have 2 log files created from 2 different locations. So, first i want to merge the 2 log files ( text files). The merging should be done line by line, based on the time stamp.

Here is the sample log,

[20/01/2009 17:32:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:33:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:34:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK

So, the script should check the time stamp in each line & merge the 2 files line by line in ascending order.

Step 2:

After merging the files, the script should read the merged file line by line & look for the keyword "HTTP/1.1 200 OK" (basically it should check if the website is available). If the keyword is missing consecutively in 2 lines then it should take the time stamp of the second line & it should check through the next consecutive lines for the keyword "HTTP/1.1 200 OK". Once it finds the next "HTTP/1.1 200 OK", it should subtract the time stamp of the "HTTP/1.1 200 OK" line with the previous time stamp noted. So that we can calculate the downtime. Like this it should sum up all the downtimes and print it to a file at the end. This will be done at the end of every month.

It will be a great help if you can help me to create a script for this task. It can be in any scripting language. Since am very new to scripting, kindly help me to create complete script for this task. I will be happy to donate for this forum, bcoz this is much important for me.

Thanks,
PK


Report •

#3
February 15, 2009 at 21:43:51
No ides how to do time math.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
February 15, 2009 at 22:39:08
Hi M2,

Thanks 4 ur response ;-)

Am trying to do the process part by part. Can you help me to create the script that will read through the log file, line by line & count the number of lines without the keyword "HTTP/1.1 200 OK" and print the count to a file. If possible it should also print the lines without the keyword.

Your help is much appreciated.

Thanks,
Pk


Report •

#5
February 15, 2009 at 23:55:26
This will combine 2 logs, sort by time and put out 2 txt files. One with the count and one with the lines not containing the string.

======================================
@echo off & setLocal EnableDelayedExpansion

copy /b log1+log2 log3 > nul
sort /+12 < log3 > log4
find /v "HTTP/1.1 200 OK" < log4 > notOK.txt
find /v /c "HTTP/1.1 200 OK" < log4 > count.txt


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
February 16, 2009 at 00:34:28
Hi M2,

Thanks a million for your help.

Since am very new to scripting just wanted to confirm if i should use the script in this way,

For example my 2 log files are in the following locations,

C:\Program Files\test\log1.txt
C:\Program Files\test\log2.txt

So, should the script be modified in this way,

======================================
@echo off & setLocal EnableDelayedExpansion

copy /b C:\Program Files\test\log1.txt+C:\Program Files\test\log2.txt log3 > nul
sort /+12 < log3 > log4
find /v "HTTP/1.1 200 OK" < log4 > notOK.txt
find /v /c "HTTP/1.1 200 OK" < log4 > count.txt

But when i try in this way, its saying the system cannot find the specified files. Sorry for asking this basic question. :-)

Thanks Again,
Pk


Report •

#7
February 16, 2009 at 01:03:53
Hi M2,

Thanks a lottttt for your help.
The script is working.

But "find /v /c "HTTP/1.1 200 OK" < log4 > count.txt" is giving the wrong count.

For example i used the sample log below, but i got the count as 5 instead of 2.

[20/01/2009 17:28:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:28:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:26:41] INFO (HTTPSClientApp.java:111) - error
[20/01/2009 17:27:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:28:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:28:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:26:41] INFO (HTTPSClientApp.java:111) - error
[20/01/2009 17:27:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK
[20/01/2009 17:28:41] INFO (HTTPSClientApp.java:111) - Website is available HTTP/1.1 200 OK

Any idea, why it is giving the wrong count ?
It gives the count from the first line of error till the last line of error.

Thanks in advance,
PK


Report •

#8
February 16, 2009 at 01:23:55
I pasted that in and got a count of 2. Beats me.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
February 16, 2009 at 01:55:51
Hi M2,

Thanks Man. It shud b some mistake on side. Will chk it.

Thanks,
Pk :-)


Report •

#10
February 24, 2009 at 19:05:52
Hi M2,

If you don't mind, can you help me to know if there is any possibility to add "A" at the end of each line of logfile1 and "B" at the end of each line of logfile2. After adding this, it can proceed with the operation the earlier script was doing.
What should i add in the below script for the requirement.

======================================
@echo off & setLocal EnableDelayedExpansion

copy /b log1+log2 log3 > nul
sort /+12 < log3 > log4
find /v "HTTP/1.1 200 OK" < log4 > notOK.txt
find /v /c "HTTP/1.1 200 OK" < log4 > count.txt


=====================================

Thanks in Advance,
PK


Report •

#11
February 25, 2009 at 03:05:07
@echo off > # & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (logfile1) do (
echo %%aA>> #
)
copy # logfile1 > nul

@echo off > #

for /f "tokens=* delims= " %%a in (logfile2) do (
echo %%aB>> #
)
copy # logfile2 > nul

del #


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#12
February 25, 2009 at 03:13:34
Hi M2,

Thanks for your great response man.

Actually i managed to do the task with the following script, but it was not that effective when using both the files simultaneously.

I prefer your crisp script. Thanks again. :-)

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_t0 is the path to where the file you want to work on is
:located and _t1 is the file name. _t3 is the string you want to tack on
:You can always use command line parameters as well by using
:Set _t0=%1, Set _t1=%2, and Set _t3=%3
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _t0=C:\test
Set _t1=SAMPLE.log
Set _t3= A
PUSHD %_t0%
If EXIST tmp1.txt del tmp1.txt
For /F "usebackq delims=" %%A in ("%_t1%") do echo %%A%_t3% >>tmp1.txt
del "%_t1%"
rename tmp1.txt "%_t1%"
For %%A in (0 1 2) do Set _t%%A=
POPD


Report •


Ask Question