FOR/f,分析但包含不需要的字符. [英] FOR /f, parses but includes unwanted characters.

查看:49
本文介绍了FOR/f,分析但包含不需要的字符.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近是该委员会的成员,并且是一般的批处理文件编程---您好!

I'm recent member to this board and batch file programming in general---hello!

几天来,我尝试以下脚本获得了圆满成功:

For several days I tried the following script with mixed success:

@回声关闭

FOR/f"tokens = 3 delims = ^:^(" %% G In('PING"hostname.com"')DO(回声%% G)

FOR /f "tokens=3 delims=^:^(" %%G In ('PING "hostname.com"') DO (Echo %%G)

该脚本的目的是对主机进行ping操作,并从ping摘要中解析出(N%丢失)中的数字(N).该脚本这样做了,但是我得到的不是数字,而是数字的"N%loss",而不仅仅是整数.我很好奇为什么会发生这种情况,以及如何使脚本仅输出百分比的整数而不输出其他字符.该批处理文件正在Win7下运行

The purpose of the script is to ping a host and parse out the number (N) in (N% loss), from the Ping summary. The script does that, but instead of a number, I get "N% loss),", and not just the integer. I'm curious why this happens and how to make the script output only the integers of the percentage and no other characters. This batch file is running under Win7

推荐答案

FOR /f "tokens=3 delims=:(%%" %%G In ('PING "hostname.com"') DO (Echo %%G)

几乎不需要在 delims 说明符中转义任何字符,除了以外,可以通过加倍-脱字号( ^ )来转义不起作用.

There's no need to escape virtually any characters in a delims specifier, EXCEPT for % which is escaped by doubling - caret (^) doesn't work.

哦-为什么?

如果解析的行仅包含((和:)作为定界符,则该行

If the line is parsed including ONLY ( and : as delimiters, then the line

数据包:已发送= 4,已接收= 4,丢失= 0(丢失0%),

有令牌

  1. 数据包
  2. 已发送= 4,已接收= 4,已丢失= 0
  3. 0%损失),

这篇关于FOR/f,分析但包含不需要的字符.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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