如何在echo中排除一些文本

时间:2015-10-26 22:37:45

标签: batch-file command-line

如何缩短此命令的输出。

nslookup myip.opendns.com resolver1.opendns.com > %~d0\bat\setup\ipaut\myip.txt

这给了我一个文本文件:

Server:  resolver1.opendns.com
Address:  208.67.222.222

Name:    myip.opendns.com
Address:  84.48.190.45

我在文本文件中实际需要的是myip.opendns.com地址。 实施例

84.48.190.45

2 个答案:

答案 0 :(得分:2)

@echo off
setlocal EnableDelayedExpansion

set "var="
for /F "tokens=2 delims=: " %%a in ('nslookup myip.opendns.com resolver1.opendns.com') do (
   if not defined var (
      set "var=%%a"
   ) else (
      set "!var!=%%a"
      set "var="
   )
)

echo %myip.opendns.com%> output.txt

答案 1 :(得分:1)

FOR /F "tokens=2 delims=: " %%G in ('nslookup myip.opendns.com resolver1.opendns.com') do >myip.txt echo %%G

解析nslookup命令的输出并将结果输出到文本文件。