0

I have a log file where the user login details are stored in below format:

 INFO ;servername;2016-02-16 01:50:12,user4@COM;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY
 INFO ;servername;2016-02-16 01:50:12,user3@COM;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY
 INFO ;servername;2016-02-17 01:50:12,user1@COM;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY
 INFO ;servername;2016-02-18 01:50:12,user2@COM;Open Analyss;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY

Requirement:

I need to get the number of users logged in on a particular date. Eg: from above log ,it should return me that 2 users(user4 and user3 ) were logged In on 2016-02-16 . Requirement is for a batch file.

  • And what have you tried so far? In its simplest form you can just use the `FIND` command. You have a very small sampling and without knowing what other things may occur it is hard to provide bullet proof piece of code. But as I said in its simplest form this would work based on your example: `find /C "2016-02-16" logfile.txt` – Squashman Feb 25 '16 at 16:39
  • And what if user4 logs in 3 times on the same day? Does that count as 3 or 1? – dbenham Feb 25 '16 at 23:33
  • @dbenham : It counts as 3. i am just looking how many time the portal has hit on a day. – user3510848 Feb 26 '16 at 11:12

3 Answers3

0
@echo off
setlocal EnableDelayedExpansion

for /F "tokens=3 delims=; " %%a in (input.txt) do (
   set "d=%%a"
   set /A "users[!d:-=_!]+=1"
)

echo Users logged in on all dates:
set users

echo/
echo Users logged in on 2016-02-16: %users[2016_02_16]%

Output:

Users logged in on all dates:
users[2016_02_16]=2
users[2016_02_17]=1
users[2016_02_18]=1

Users logged in on 2016-02-16: 2

You may review a detailed explanation of the array method used in this solution at: Arrays, linked lists and other data structures in cmd.exe (batch) script

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks @Aacini. It works perfectly. I will go through the link you provided. Thanks again . – user3510848 Feb 26 '16 at 11:10
  • If you think this answer aids you, you may select its check mark, and later, when you have enough rep, upvote it. This is the way we use in this site to say "thanks"... – Aacini Feb 26 '16 at 14:30
0

Method posted by Aacini will work, but the FOR command is rather slow when processing larger amounts of data. If you have very large files and only need to find the number of users on one particular date, I would recommend using the FIND command, which is faster, because the iteration takes place completely within the command.

If you assume that dates only appear within the date data field in the file, you can use FIND /C "date to find" path\filename This will give a single line as output, including the file name and ending with the number of found lines.

If you suspect that dates may occur in other data fields in the file, the following code may be used:

@echo off
SETLOCAL enabledelayedexpansion
SET srcstring=insert your date here
SET file=insert your path and filename here
FOR /F "tokens=3 delims=; " %%F IN ('FIND "%srcstring%" %file%') DO (
IF "%%F"=="%srcstring%" (SET /A "count+=1")
)
echo %count% users found on date %srcstring%

It uses the faster command FIND to find all lines where the date appears, then uses the slower FOR to check that the date appears in the right data field on the line. Since FORonly iterates through those lines that contain the date, it will be significantly faster.

Alpha_Pi
  • 383
  • 2
  • 13
0

You never state whether there are non-login lines of a different format within the log. I'm going to assume there are.

You also never state what portion of the login line is static. I'm going to assume only the date/time and username change. So this code will only count successful connections. You will have to adjust the code to match your actual conditions.

If all you want to do is print out the count, then you can use FINDSTR with a regular expression to select the relevant rows, and pipe that to FIND to count the rows.

The code below expects the desired date to be passed as a parameter in YYYY-MM-DD format

@echo off
:CountSuccessfulLogins  date
set "dt=%~1"
set "prefix= INFO ;servername;"
set "suffix= .*;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY"
findstr /rc:"^%prefix%%dt%%suffix%$" test.log | find /c /v ""
exit /b

If you want the count in a variable, then capture the output with FOR /F

@echo off
:CountSuccessfulLogins  date
set "dt=%~1"
set "prefix= INFO ;servername;"
set "suffix= .*;Open Analysis;H5SeLhbEVUKA0ml-kFUw5-151708d6b3Y8Tz;/Metric/core/corePortal;Success;151708fdfgY"
for /f %%N in ('findstr /rc:"^%prefix%%dt%%suffix%$" test.log ^| find /c /v ""') do set cnt=%%N
echo %cnt% Successful Logins on %dt%
exit /b
dbenham
  • 127,446
  • 28
  • 251
  • 390