Link to home
Create AccountLog in
Avatar of aarun_tcs
aarun_tcs

asked on

Please help me to trim text file through batch file.

Please help me to trim text file through batch file.

I have one log file. The log file content is given below.

"CN=496403,OU=Users,OU=Chennai1,OU=Chennai,OU=India,DC=SNR003,DC=com"
"CN=496402,OU=Users,OU=Siruseri,OU=Mumbai,OU=India,DC=SNR003,DC=com"
"CN=123456,OU=Users,OU=Velachery,OU=Mumbai,OU=India,DC=SNR003,DC=com"

i want to trim the content in the text file.
the final output file shud be

496403
496402
123456
Avatar of Darr247
Darr247
Flag of United States of America image

For clarity, will it always be 6 numbers before the first comma, or could it be 5 numbers, or 7 numbers? Or might there be letters sometimes, too?
Avatar of aarun_tcs
aarun_tcs

ASKER

it may have 5 or 8 letters before first comma..  its not only numbers it have have letters also...
the thing is first i need to remove "CN= and then i need to remove the letters from first comma. ex: ,OU=Users,OU=Chennai1,OU=Chennai,OU=India,DC=SNR003,DC=com"
Try this:
@echo off
setlocal
set InFile=C:\Temp\Logfile.log
set OutFile=C:\Temp\UserIDs.log
if exist "%OutFile%" del "%OutFile%"
for /f "usebackq tokens=2 delims=,=" %%a in ("%InFile%") do (
	>>"%OutFile%" echo %%a
)

Open in new window

Powershell routine to do this.

# how to use this
# filename.ps1 inputfilename > outputfilename
# note no error checking is left for the user excersize
$infile = args[1]
$text = get-content $infile
foreach ($line in $text) {
$line1 = $line.trimstart("""CN=")
$pos = $line1.IndexOf(",")
$wanted = $line1.Substring(0, $pos) 
$wanted
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thanks for the help