Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
152 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...