Windows package for the tab delimiter of the FOR command

I am trying to use the Windows package FOR command .

How to specify a TAB character as a separator? By default, this is the space and tab, but I want only the tab.

I tried this, but it does not work:

FOR /F "tokens=3 delims=^T" %i in (test.txt) do @echo %i 

It seems to break into a T symbol instead of a bookmark. I also tried a literal inserted on the command line, but I get a syntax error.

+4
source share
4 answers

In the line of options quoted, you will need a literal tab character. The TAB literal works both in a batch file and on the command line.

If you are trying to enter TAB at the command line, then completing the file name can frustrate your efforts. You can disable name completion using CMD /F:OFF . You can then enter the literal character TAB.

You may also encounter problems when trying to enter TAB into a batch script depending on the text editor used. Some programmer editors have settings that automatically convert TAB to spaces. You will need to make sure your editor does not.

So you just need to change the line "tokens=3 delims=^T" to use the literal TAB instead of ^T

+8
source

Ngaaa .. talk about daytime headache ...

I had the same problem today ... I need to parse the lines with FOR / F, where each line had a series of sentences (each with embedded spaces, possibly) with each sentence separated by TAB.

My problem is that my coding standards (even for BATCH files) forbid actual TAB characters from source code (our editors forbid putting TAB in and our ctrl Version tools forbid any CheckIn of any text file with TAB or WTR [from white to the right].)

So this is a hack, but it works on XP (hanging head ... deep shame ...)

 SET REG_CPU="HKLM\Hardware\Description\System\CentralProcessor\0" REG.EXE QUERY %REG_CPU% /V IDENTIFIER | FIND "REG_SZ" > RegCPUid.Tmp for /F "tokens=2 delims=rR" %%i in (RegCPUid.Tmp) do SET TAB=%%~i 

This works on XP, but I have not tested Win7 or Win7 [WoW64].

Temporary contents of RegCPUid.Tmp is ONE LINE that looks like

 " IDENTIFIER<tab>REG_SZ<tab>x86 Family 6 Model 37 Stepping 2" 

The FOR command simply pulls out a single TAB between the two "R" s IDENTIFIER and REG_SZ and sets the TAB environment variable for it.

Then my script can do the whole FOR / F loop that it wants, ONLY the tab character as a delimiter (and NOT a space) ...

 for /F "tokens=1-3* delims=%TAB%" %%i in (Sentences.txt) do ECHO The THIRD sentence is %%k 

All this allows me to NOT have any hard-coded TAB characters in the BATCH file.

-dave

+4
source

The literal tab character seems to work, but you should start the cmd process with /F:ON . Without this, even inserting a tab character does not work.

+1
source

According to this website http://ss64.com/nt/for_f.html , the default separator is a tab, so try it without the delims parameter.

0
source

All Articles