0

I have a problem to parse a text as a result of "net user" command

ABC                      ABCDE                    ABCDEFG                  
ABDCS HFJ                ATi                      CObdnsen 

to single users in a separate lines in batch script.

my code is this :

FOR /F "tokens=1,2,3 delims= " %%A in (test.txt) do echo %%A & echo %%B & echo %%C

the problem is the second line of my text that contains a username with space in the middle.

Note that i want this result :

ABC
ABCDE
ABCDEFG
ABDCS HFJ
ATi
CObdnsen

so what is the solution?

Kara
  • 6,115
  • 16
  • 50
  • 57
Dragon
  • 1
  • 4

3 Answers3

2

That looks to be fixed width columns, 25 characters per column. You could load each line into a variable and use substring operations to get the values.

@echo off
setlocal enableDelayedExpansion
for /f delims^=^ eol^= %%A in (test.txt) do (
  set "ln=%%A"
  echo col1=[!ln:~0,25!]
  echo col2=[!ln:~25,25!]
  echo col3=[!ln:~50!]
  echo(
)

But that leaves you with the problem of removing trailing spaces from the end of each value. It is doable, but not easy with batch. My output from the above script encloses the values within square brackets so you can easily see the trailing space issue.

Instead of messing with the issue of removing trailing spaces, I would use my REPL.BAT utility to transform the data from fixed width to delimited format. REPL.BAT is a hybrid JScript/batch utility that performs a regular expression search/replace operation on stdin and writes the result to stdout. It is pure script that will run on any modern Windows machine from XP onward without needing any 3rd party executables. Full documentation is embedded within the script.

I would use REPL.BAT to insert a delimiter after the 25th and 50th characters. I would then use another REPL.BAT to strip out any spaces that precede the delimiter, and then a normal FOR /F can safely parse the values. I chose to use a pipe (|) as the delimiter.

@echo off
for /f "eol=| delims=| tokens=1-3" %%A in (
  'type test.txt ^| repl "^(.{25})(.{25})" "$1|$2|" ^| repl " *\|" "|"'
) do (
  echo col1=[%%A]
  echo col2=[%%B]
  echo col3=[%%C]
  echo(
)

If you know that no value contains consecutive spaces, and all values are separated by at least 2 spaces, then you can get by with a single REPL that replaces 2 or more spaces with a delimiter

@echo off
for /f "eol=| delims=| tokens=1-3" %%A in (
  'type test.txt ^| repl " {2,}" "|"'
) do (
  echo col1=[%%A]
  echo col2=[%%B]
  echo col3=[%%C]
  echo(
)
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Yea... thats works fine... i had found this way but i was wondered why i cant do that with delims... Thanks alot btw. – Dragon May 05 '14 at 13:37
0
@echo off
    setlocal enableextensions

    for /f "tokens=*" %%a in ('net user^|find "  "') do (
        set "u=%%a"
        setlocal enabledelayedexpansion
        set "u=!u:  =/!"
        set "u=!u:/ =/!"
        for /f "tokens=1 delims=/" %%b in ("!u!") do echo %%b
        endlocal   
    )

    endlocal

Not bullet proof. Will fail for user names with two spaces in it or (probably, not tested) with names of 24 characters or more (just to name two).

MC ND
  • 69,615
  • 8
  • 84
  • 126
0

Thanks MC ND for your great idea... But your Answer should be edited like below for the best result :

@echo off
    setlocal enableextensions

    for /f "tokens=*" %%a in ('net user^|find "     "') do (
        set "u=%%a"
        setlocal enabledelayedexpansion
        set "u=!u:  =/!"
        set "u=!u:/ =/!"
        for /f "tokens=1,2,3 delims=/" %%b in ("!u!") do echo %%b & echo %%c & echo %%d
        endlocal   
    )

    endlocal

Note that the delimiter is "5 spaces". because the most allowed user length is 20 character in windows. and the distance between the columns are 25 character.. so we have at least 5 spaces as a good delimiter. and we are not worry about spaces in the middle of a username. and at last your should parse three tokens . Not only 1 token. Thanks for your attention guys.

Dragon
  • 1
  • 4