How to find substrings in a log file and increment a counter using dos batch file

Can someone help me in creating a windows batch file to find the substring from the log file. An example log.log file is shown below.

ID,Date,Time,Description,IP Address,Host Name,MAC Address
10,02/21/14,00:29:45,Assign,172.20.55.50,PC1,123456789AB1,
31,02/21/14,00:29:45,DNS Update,172.20.55.50,PC1,123456789AB1,
10,02/21/14,00:29:45,Assign,172.30.55.50,PC2,123456789AB2,
31,02/21/14,00:29:45,DNS Update,172.30.55.50,PC1,123456789AB2,
10,02/21/14,00:29:45,Assign,172.20.56.60,PC3,123456789AB3,
10,02/21/14,00:29:45,Assign,172.30.55.60,PC4,123456789AB4,
**11,02/21/14,00:30:45,Assign,172.30.55.10,PC2,123456789AB5,**
**11,02/21/14,00:30:46,Assign,172.30.55.10,PC2,123456789AB5,**
**31,02/21/14,00:00:37,DNS Update Failed,172.17.110.13,TAR-CAR-051180L.WTPK.local,-1,**

This is a DHCP log file. The goal is to count the number of new destination IP addresses (whose identifier is 10) and the number of repeated IP requests (whose identifier is 11).

For ID 10, if IP starts at 172.20.55 or 172.20.56, it should increment the "NewPoolA" counter, and if IP starts at 172.30.55 or 172.30.56, it should increment in "NewPoolB".

ID 11, IP 172.20.55 172.20.56, "RenewPoolA", IP 172.30.55 172.30.56, "RenewPoolB".

, ,

@echo off 
Setlocal EnableDelayedExpansion

set /a NewPoolA=0
set /a NewPoolB=0
set /a RenewPoolA=0
set /a RenewPoolB=0

for /F "tokens=1-6 delims=," %%a in (log.log) do (
    if %%a equ 10 (
        rem if %%e contains 172.20.55 (
            set /a NewPoolA += 1
            goto someLabel
        )
        rem else if %%e contains 172.20.56 (
            set /a NewPoolA += 1
            goto someLabel
        )
        rem else if %%e contains 172.30.55 (
            set /a NewPoolB += 1
            goto someLabel
        )
        rem else if %%e contains 172.30.56 (
            set /a NewPoolB += 1
            goto someLabel
        )
        rem -------- if id 10 and not match any condition then
        goto someLabel
    ) else if %%a equ 11 (
        rem if %%e contains 172.20.55 (
            set /a RenewPoolA += 1
            goto someLabel
        )
        rem else if %%e contains 172.20.56 (
            set /a RenewPoolA += 1
            goto someLabel
        )
        rem else if %%e contains 172.30.55 (
            set /a RenewPoolB += 1
            goto someLabel
        )
        rem else if %%e contains 172.30.56 (
            set /a RenewPoolB += 1
            goto someLabel
        )
        rem -------- if id 11 and not match any condition then
        goto someLabel
    )
)
echo Total new request in Pool A is %NewPoolA%
echo Total renewal request in Pool A is %RenewPoolA%

echo Total new request in Pool B is %NewPoolB%
echo Total renewal request in Pool B is %RenewPoolB%

, . dos batch.

- . 80 , . 100 000 . . , , "if" .

, MAC-. , MAC-.

+3
4

, :

@echo off
for /f %%a in ('type "file.csv"^|findstr "^10," ^|findstr ",172.20.55 ,172.20.56"^|find /c /v ""') do set AAA-NewPoolA=%%a
for /f %%a in ('type "file.csv"^|findstr "^10," ^|findstr ",172.30.55 ,172.30.56"^|find /c /v ""') do set AAA-NewPoolB=%%a
for /f %%a in ('type "file.csv"^|findstr "^11," ^|findstr ",172.20.55 ,172.20.56"^|find /c /v ""') do set AAA-ReNewPoolA=%%a
for /f %%a in ('type "file.csv"^|findstr "^11," ^|findstr ",172.30.55 ,172.30.56"^|find /c /v ""') do set AAA-ReNewPoolB=%%a
set aaa
pause
+1

, , , , . array :

@echo off
setlocal EnableDelayedExpansion

rem Accumulate results for all ID.IP (first 3 groups) combinations
for /F "skip=1 tokens=1,5-7 delims=,." %%a in (log.log) do (
   set /A requests[%%a.%%b.%%c.%%d]+=1
)

rem Get desired results
set /A NewPoolA = requests[10.172.20.55] + requests[10.172.20.56]
set /A NewPoolB = requests[10.172.30.55] + requests[10.172.30.56]

set /A RenewPoolA = requests[11.172.20.55] + requests[11.172.20.56]
set /A RenewPoolB = requests[11.172.30.55] + requests[11.172.30.56]

echo Total new request in Pool A is %NewPoolA%
echo Total renewal request in Pool A is %RenewPoolA%

echo Total new request in Pool B is %NewPoolB%
echo Total renewal request in Pool B is %RenewPoolB%

, , 80 . :

@echo off
setlocal EnableDelayedExpansion

rem Accumulate results for all ID.IP (first 3 groups) combinations
for /F "skip=1 tokens=1,5-7 delims=,." %%a in (log.log) do (
   set /A requests[%%a.%%b.%%c.%%d]+=1
)

rem Get and show desired results from a long pool definition list
for %%A in ("NewPoolA=10 172.20.55+10 172.20.56"
            "NewPoolB=10 172.30.55+10 172.30.56"
            "RenewPoolA=11 172.20.55+11 172.20.56"
            "RenewPoolB=11 172.30.55+11 172.30.56") do (
   for /F "tokens=1-5 delims==+ " %%a in (%%A) do (
      set /A %%a=requests[%%b.%%c]+requests[%%d.%%e]
      echo Total %%a request is !%%a!
   )
)

:

C:\> test
Total NewPoolA request is 2
Total NewPoolB request is 2
Total RenewPoolA request is 0
Total RenewPoolB request is 1

"NewPoolA" " A", ! ;-)

, . , . MAC-, MAC-, -1, , IP-.

@echo off
setlocal EnableDelayedExpansion

rem Accumulate results for all ID.IP(first 3 groups).MAC_addr combinations
rem NEW: Ignore MAC_addresses equal to -1
for /F "skip=1 tokens=1,5,7 delims=," %%a in (log.log) do (
   if "%%c" neq "-1" for /F "tokens=1-3 delims=." %%i in ("%%b") do (
      set /A requests[%%a.%%i.%%j.%%k.%%c]+=1
   )
)

rem Get and show desired results from a long definition list
rem NEW: Only accumulate requests for unique MAC addresses (count=1)
for %%A in ("NewPoolA=10 172.20.55+10 172.20.56"
            "NewPoolB=10 172.30.55+10 172.30.56"
            "RenewPoolA=11 172.20.55+11 172.20.56"
            "RenewPoolB=11 172.30.55+11 172.30.56") do (
   for /F "tokens=1-5 delims==+ " %%a in (%%A) do (
      set %%a=0
      for /F "tokens=2 delims==" %%x in ('set requests[%%b.%%c 2^>NUL') do (
         if %%x equ 1 set /A %%a+=1
      )
      for /F "tokens=2 delims==" %%x in ('set requests[%%d.%%e 2^>NUL') do (
         if %%x equ 1 set /A %%a+=1
      )
      echo Total %%a request is !%%a!
   )
)
+1

. ,

@echo off
setlocal

set "NewPoolA=0"
set "NewPoolB=0"
set "RenewPoolA=0"
set "RenewPoolB=0"

for /F "tokens=1-6 delims=," %%a in (log.log) do (
  if "%%~a" equ "10" (
    for /f %%b in ('echo "%%e"^|Findstr /c:"172.20.55" /c:"172.20.56"') do (
      set /a NewPoolA+=1    
    )
  )
)
echo Total new request in Pool A is %NewPoolA%
0
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $ or #
For %%b IN ($ #) DO FOR  /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
FOR /f "tokens=1-4delims=. " %%a IN (q21935716.txt) DO (
 IF "%%d"=="" (SET $%%a=%%b) ELSE (SET #%%b.%%c.%%d=%%a)
)

FOR /f "tokens=1,5,6,7delims=.," %%a IN (q21935716.log) DO (
 IF DEFINED $%%a IF DEFINED #%%b.%%c.%%d (SET /a !$%%a!!#%%b.%%c.%%d!+=1)
)

FOR /f "tokens=2delims==" %%a IN ('set $') DO SET %%a

GOTO :EOF

, , 80 .

q21935716.txt :

10 newpool
11 renewpool
A 172.20.55
A 172.20.56
B 172.30.55
B 172.30.56

, + , + IP.

- eny, $ #.

The next step is to read the file q21935716.txtusing .or spaceas delimiters. This means that the pool strings will have 2 tokens and ip strings 4. Then the variables $ poolname and #ip will be created, containing the identifier and pools, respectively.

Then it is a matter of reading the log file, delimiters . ,or space, which means that tokens 1,5,6,7 are of interest. Only if $ ID and #IP exist, is this the line of interest. In tose lines, you need to increase the poolpoolsection pool.

The latter forlists the names of the pools.

0
source

All Articles