Striping variable text out of a text file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Striping variable text out of a text file?

#1 Post by mr_Frank » 27 Jul 2012 07:57

Hi,

I've got a script which is outputting a load of user attributes from an Active Directory.

I've got to it to a point where i have the contents of the text file as follows:
>sAMAccountName: username1
>sAMAccountName: username2
>sAMAccountName: username3
>sAMAccountName: username4
>sAMAccountName: username5
...
..
.

Is there some why to strip out the ">sAMAccountName: " bit so i'm just left with the usernames?

Thanks

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Striping variable text out of a text file?

#2 Post by Squashman » 27 Jul 2012 08:19

Post the script you are using now and we can simplify it for you.

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#3 Post by mr_Frank » 27 Jul 2012 08:23

Hi,

I'm just doing a :

type adoutput.txt | find "sAM"

to get me that output

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Striping variable text out of a text file?

#4 Post by Squashman » 27 Jul 2012 09:39

And the output is exactly as you posted? It has a > at the beginning of each line?

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#5 Post by mr_Frank » 27 Jul 2012 09:45

yep, the actual file contents are as follows, each line has the '>' symbol at the beginning:

>cn: John Smith
>homeDirectory: \\server\username1
>sAMAccountName: username1

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

Re: Striping variable text out of a text file?

#6 Post by foxidrive » 27 Jul 2012 10:40

This should do it. (untested)

Code: Select all

@echo off
for /f "tokens=1,*" %%a in (' type adoutput.txt ^| find "sAM" ') do echo %%b

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Striping variable text out of a text file?

#7 Post by Squashman » 27 Jul 2012 10:46

foxidrive wrote:This should do it. (untested)

Code: Select all

@echo off
for /f "tokens=1,*" %%a in (' type adoutput.txt ^| find "sAM" ') do echo %%b

I was thinking that maybe it should find "sAMAccountName" in case some other line might have those 3 letters in it.

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#8 Post by mr_Frank » 30 Jul 2012 01:36

Perfect works a treat - cheers all :D

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#9 Post by mr_Frank » 30 Jul 2012 02:52

do you know how the same result could be obtained if the data I wanted was at the end of a line of text like:

\\domain.com\customer\region\site\folder\username1
\\domain.com\customer\region\site\folder\username2
\\domain.com\customer\region\site\folder\username3
\\domain.com\customer\region\site\folder\username4

So I want to be left with just username1,2,3,4 as output?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Striping variable text out of a text file?

#10 Post by abc0502 » 30 Jul 2012 03:46

Try This:

Code: Select all

@echo off
for /f "tokens=1,2,3,4,5* delims=\" %%a in ('type "adoutput.txt"') do echo %%f

%%a is for domain.com
%%b is for customer
%%c is for region
%%d is for site
%%e is for folder
%%f is for usernameX

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#11 Post by mr_Frank » 30 Jul 2012 04:04

Spot on - awesome - thanks :D

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

Re: Striping variable text out of a text file?

#12 Post by foxidrive » 30 Jul 2012 05:21

In your case you can also use a documented command like this, to get the usernames:

Code: Select all

@echo off
for /f "delims=" %%a in (file.txt) do echo %%~nxa

mr_Frank
Posts: 10
Joined: 27 Jul 2012 07:48

Re: Striping variable text out of a text file?

#13 Post by mr_Frank » 30 Jul 2012 05:56

even better - nice one :-D

Post Reply