tokens and delims expert help needed

2019-12-16 21:59发布

问题:

I have been using batch a lot lately to great ends in my work and also little bits of power shell here and there to great effect.

One thing that always baffles me is the tokens and delims concepts when working through loop etc. I have watched you tube videos and read the classic rob van pages on it. I kind of get it, but when I need to use it's not basic.

I have an example cropped up where if some one was kind enough to explain how you choose the number of tokens and delims it would be really helpful. I need to extrapolate a PC host-name from various columns in a web page. I can copy the text into a text pad and would like to "echo out" into another file just the computer names.

24 Nov 2016 09:45 GMT   194.176.105.132 United Kingdom  ID006962.CENTRAL    3.10.6.0    Remove
24 Nov 2016 09:44 GMT   194.176.105.154 United Kingdom  ID006976.CENTRAL    3.10.5.0    Remove
24 Nov 2016 09:43 GMT   194.176.105.146 United Kingdom  ID007634.CENTRAL    3.10.6.0    Remove
24 Nov 2016 09:41 GMT   194.176.105.138 United Kingdom  ID006961.CENTRAL    3.10.6.0    Remove
24 Nov 2016 09:28 GMT   194.176.105.132 United Kingdom  ID007643.CENTRAL    3.10.5.0    Remove

The computer names are the ID0006962 numbers BUT I dont need the trailing .CENTRAL. From this text I just need another text file with the hostnames. I can do all that part, it's just the extrapolating data elements. Sometimes the lists are massive and it would be nice just save into a text file run a for loop against it and spit out a another text file with just the host-names. I think If I can get my head round this a little more I could apply the knowledge to other things I need to do as well. Many thanks for any tips !

sample output (i'd like) in another text file:-

ID006967                                                             
ID007566                                                               
ID007567                                                              
ID006976    
ID007643     

edit how can I also get the 3.10.6.0 etc next to the host name

ID006967     3.10.6.0                                                         
ID007566     3.10.6.0                                                          
ID007567     3.10.6.0                                                         
ID006976     3.10.6.0
ID007643     3.10.6.0

I've had a go but got lost here's what I thought...

@echo off

> "%~dp0test.txt" (
    for /F "usebackq tokens=4,5 delims= " %%J in ("%~dp0rob.txt") do @(
        for /F "tokens=1,3,4,5,6 delims=." %%I in ("%%J") do @echo(%%I
    )
)

so my thinking (LOL)...I want the 4,5 tokens (columns 4 and 5) extracting into %%J I want the next loop to echo the additional tokens as well as the host name, so still using '.' as a delimiter in this test with rows 4 and 5 I believe there are now 6 tokens to choose from....but the out put still just show the host name...what don't I get

ID006962(t1).CENTRAL(t2) 3(t3).10(t4).6(t5).0(t6)

回答1:

Two possible solutions for you.

The first uses the same idea as aschipfl's answer just slightly more refined, remember delims=TAB:

@(For /f "UseBackQTokens=4Delims=   " %%A In ("pagedata.txt"
) Do @Echo=%%~nA)>another.txt

The second uses the period as a delimiter:

@(For /f "UseBackQTokens=4Delims=." %%A In ("pagedata.txt"
) Do @For %%B In (%%A.x) Do @If Not "%%~xB"=="" Echo=%%~nB)>another.txt

Just change the input filename of pagedata.txt and the ouptput filename of another.txt to suit your purposes.



回答2:

Your data seem to be a TAB-separated table consisting of six columns. The partial string you want to extract is located in the fourth column. So let us use a for /F loop to get the fourth token (note that there is a single TAB character after delims=):

for /F "usebackq tokens=4 delims=   " %%J in ("inputfile.txt") do echo(%%J

This extracts strings like ID0006962.CENTRAL. Now let us split this string by another for /F loop:

for /F "tokens=1 delims=." %%I in ("ID0006962.CENTRAL") do echo(%%I

Then let us use redirection to output the data to a text file:

> "outputfile.txt" echo(ID0006962

Finally let us put all those things together to get a working script:

> "outputfile.txt" (
    for /F "usebackq tokens=4 delims=   " %%J in ("inputfile.txt") do @(
        for /F "tokens=1 delims=." %%I in ("%%J") do @echo(%%I
    )
)


回答3:

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q40784266.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO CALL :process %%a
)>"%outfile%"

GOTO :EOF

:process
FOR /f "tokens=1,2delims=." %%q IN ("%1") DO IF "%%r" equ "CENTRAL" (
 echo %%q
 GOTO :eof
) ELSE (
 SHIFT
 GOTO process
)

GOTO :eof

You would need to change the setting of sourcedir to suit your circumstances. You would need to change the settings of sourcedir and destdir to suit your circumstances.

I used a file named q40784266.txt containing your data for my testing.

Produces the file defined as %outfile%

Read each line of the input file to %%a and call :process passing the entire line as parameters.

:process takes the first parameter supplied (24) and attempts to parse into tokens using . as the delimiter. If the second token (in %%r) is not "CENTRAL" then shift the parameters and try again.

"Nov","2016","09:45","GMT" each do not have a second token, so shift
"194.176.105.132" has a second token of "176" which doesn't match, so shift
"United","Kingdom" no second token, so shift
"ID006962.CENTRAL" has a second token, it matches "CENTRAL", so print the first,"ID006962" and go to the end, abandoning the remaining parameters "3.10.6.0" and "Remove"

Note that a string such as Central African Republic will not fail the testing as the match for CENTRAL is case-sensitive and "Central" has no second token anyway.

Note that some syntax elements are layout-sensitive. Cut-and-paste is recommended using a text-editor, not a word-processor which may silently "format" the code. Note that NOTEPAD tends also to format the code. I use EDITPLUS.