Empty tokens in For /f?
I received email with following question from Otto:
I have file with delimiters, however not every token have values. For example following file:
test1;test2;test3
t1;;t3
;te2;te3
tes1;tes2;
If I try to use For /f to parse it (For /f "usebackq delims=; tokens=1,2,*" %%i IN (`Type Test.txt`) Do Echo a=%%i b=%%j c=%%k, following is output:
a=test1 b=test2 c=test3
a=t1 b=t3 c=
a=te2 b=te3 c=
a=tes1 b=tes2 c=
As you can see, For /f ignores empty tokens... So how to solve it?
Only solution that came to my mind was to recommend using search&replace utility to replace these values first... However after some time I thought - "Hey, this is task ideal for regex replacement".
So I tried yesterday to write EmptyTokenParser.cmd that should be able to handle it (using SED from GnuWin32).
@Echo off
Set Int.File=%~1
Set Int.Delimiter=%~2
type "%Int.File%" | sed s/"^%Int.Delimiter%"/"#empty#%Int.Delimiter%"/g;s/"%Int.Delimiter%%Int.Delimiter%"/"%Int.Delimiter%#Empty#%Int.Delimiter%"/g;s/"%Int.Delimiter%$"/"%Int.Delimiter%#Empty#/g
Usage is quite easy - EmptyTokenParser "Test.txt" ";" and result will appear like this:
test1;test2;test3
t1;#Empty#;t3
#empty#;te2;te3
tes1;tes2;#Empty#
You must download SED to make it work. SED among many GREAT command line utilities can be found at http://gnuwin32.sourceforge.net/.
So the final script:
For /f "usebackq delims=; tokens=1,2,*" %%i IN (`EmptyTokenParser "Test.txt" ";"`) Do Echo a=%%i b=%%j c=%%k
Output?
a=test1 b=test2 c=test3
a=t1 b=#Empty# c=t3
a=#empty# b=te2 c=te3
a=tes1 b=tes2 c=#Empty#
P.S.: Do you also use your blogs to write reminders? ;)