Striping variable text out of a text file?
Moderator: DosItHelp
Striping variable text out of a text file?
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
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
Re: Striping variable text out of a text file?
Post the script you are using now and we can simplify it for you.
Re: Striping variable text out of a text file?
Hi,
I'm just doing a :
type adoutput.txt | find "sAM"
to get me that output
I'm just doing a :
type adoutput.txt | find "sAM"
to get me that output
Re: Striping variable text out of a text file?
And the output is exactly as you posted? It has a > at the beginning of each line?
Re: Striping variable text out of a text file?
yep, the actual file contents are as follows, each line has the '>' symbol at the beginning:
>cn: John Smith
>homeDirectory: \\server\username1
>sAMAccountName: username1
>cn: John Smith
>homeDirectory: \\server\username1
>sAMAccountName: username1
Re: Striping variable text out of a text file?
This should do it. (untested)
Code: Select all
@echo off
for /f "tokens=1,*" %%a in (' type adoutput.txt ^| find "sAM" ') do echo %%b
Re: Striping variable text out of a text file?
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.
Re: Striping variable text out of a text file?
Perfect works a treat - cheers all 

Re: Striping variable text out of a text file?
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?
\\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?
Re: Striping variable text out of a text file?
Try This:
%%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
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
Re: Striping variable text out of a text file?
Spot on - awesome - thanks 

Re: Striping variable text out of a text file?
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
Re: Striping variable text out of a text file?
even better - nice one 
