Find Word After A String

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
GHamilton
Posts: 5
Joined: 21 Jan 2013 19:24

Find Word After A String

#1 Post by GHamilton » 21 Jan 2013 22:13

Hi all,

I was wondering if it's possible to create a script to find a word at the end of a sentence or in the middle of a sentence that could be random.

The reason I'm trying to do this is because I have a logon script that writes when a user logs on and off through the %username% variable. I want to use Batch to select only the username after x (username) logged on at xx:xx (time), the reason I need something that finds the word before/after a sentence is because the logon script is randomised so the username isn't one per file.

Also, this would be helpful for so many other possible reasons.

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Word After A String

#2 Post by foxidrive » 22 Jan 2013 01:25

Can you give us an example of an entry?

GHamilton
Posts: 5
Joined: 21 Jan 2013 19:24

Re: Find Word After A String

#3 Post by GHamilton » 22 Jan 2013 03:10

Sure, here's an example:

Logged off user1 at dd-mm-yy at xx:xx:xx (hours, minutes, seconds)
Logged on user2
Logged off user2
Logged on user3

In all the above examples the date and time is included.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Word After A String

#4 Post by foxidrive » 22 Jan 2013 03:29

GHamilton wrote:Sure, here's an example:

Logged off user1 at dd-mm-yy at xx:xx:xx (hours, minutes, seconds)
Logged on user2
Logged off user2
Logged on user3

In all the above examples the date and time is included.


From that sample, what do you want to extract?

GHamilton
Posts: 5
Joined: 21 Jan 2013 19:24

Re: Find Word After A String

#5 Post by GHamilton » 22 Jan 2013 05:39

Sorry, I should clarify. What I want to do is get the last user logged onto a machine and add that to a text file and name it that username.

The layout of the logs on the server is just like that example that I included in my previous post.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Word After A String

#6 Post by foxidrive » 22 Jan 2013 05:48

So in the log above you want 'user3' - is that all?

You want one user per log file?

GHamilton
Posts: 5
Joined: 21 Jan 2013 19:24

Re: Find Word After A String

#7 Post by GHamilton » 22 Jan 2013 06:04

Essentially yes but I don't want to change the logon script, just split it later plus this could be used for other things.

What I really want is a script that will display the word which the user nay not know, after or before a sentence or phrase that they do know that would be in the script.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find Word After A String

#8 Post by foxidrive » 22 Jan 2013 06:11

GHamilton wrote:Essentially yes but I don't want to change the logon script, just split it later plus this could be used for other things.


This batch file should get the last username

Code: Select all

@echo off
for /f "tokens=3" %%a in ('type "logfile.log" ^|findstr /i /c:"Logged on "') do set "user=%%a"
echo "%user%">"%user%.txt"


What I really want is a script that will display the word which the user nay not know, after or before a sentence or phrase that they do know that would be in the script.


You'll have to give an example of the text you want to search and also list the part you want to find.

GHamilton
Posts: 5
Joined: 21 Jan 2013 19:24

Re: Find Word After A String

#9 Post by GHamilton » 22 Jan 2013 06:41

Thanks so much for that, I should be able to modify that for other uses.

Thanks so much for all your help, foxidrive.

Post Reply