令牌和delims专家需要的帮助 [英] tokens and delims expert help needed

查看:140
本文介绍了令牌和delims专家需要的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在工作中经常使用批处理,以达到最佳效果,并且在这里和那里也使用了很少的电源外壳,从而产生了很大的效果.

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.

一遍让我感到困惑的事情是通过循环等工作时的记号和delims概念.我已经看过您观看视频并阅读经典的rob van页面.我有点理解,但是当我需要使用它时,这不是基本的.

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.

我举了一个例子,如果有人能很好地解释您如何选择代币和delims的数量,那将非常有帮助.我需要从网页的各个列中推断PC主机名.我可以将文本复制到文本板中,并想将计算机名称仅回显"到另一个文件中.

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

计算机名称是 ID0006962 数字,但我不需要结尾的 .CENTRAL .从此文本中,我只需要另一个带有主机名的文本文件.我可以做所有这部分,只是推断数据元素.有时列表很大,只需要保存到文本文件中,然后对它运行for循环,然后吐出另一个仅包含主机名的文本文件就可以了.我认为,如果我能对此有所了解,可以将知识应用到我也需要做的其他事情上.非常感谢您的提示!

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     

编辑如何在主机名旁边获取3.10.6.0等

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
    )
)

所以我的想法(LOL)...我希望将4,5个令牌(第4和5列)提取到%% JI中,希望下一个循环回显其他令牌以及主机名,因此仍使用' ."作为该测试中第4行和第5行的定界符,我相信现在有6个标记可供选择....但是输出仍然只显示主机名...我不知道

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)

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

推荐答案

为您提供两种可能的解决方案.

Two possible solutions for you.

第一个使用与aschipfl的答案相同的思想,只是稍微精致一些,请记住delims = TAB :

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

只需更改pagedata.txt的输入文件名和another.txt的输出文件名即可.

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

这篇关于令牌和delims专家需要的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆