【问题标题】:Skip multiple lines in a batch script在批处理脚本中跳过多行
【发布时间】:2014-09-17 17:44:18
【问题描述】:

我只想从命令的输出中打印出某一行。 让我们以ipconfig 命令为例。这会返回很多行。

Windows IP Configuration


Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : ab80::456d:123e:5ae5:9ab6%15
   IPv4 Address. . . . . . . . . . . : 192.168.1.33
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Local Area Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

我只想打印第 11 行。

我尝试了以下

FOR /F "skip=10 delims=" %G IN ('IPCONFIG') DO @ECHO %G

这只会跳过前 10 行并打印其余行。

   Default Gateway . . . . . . . . . : 192.168.1.1
Ethernet adapter Local Area Connection:
   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

如何只打印第 11 行?

【问题讨论】:

    标签: windows batch-file cmd command-prompt


    【解决方案1】:

    离开循环

    FOR /F "skip=10 delims=" %G IN ('IPCONFIG') DO @ECHO %G & goto done
    :done
    

    已编辑从单个命令行获取命令输出的第 11 行

    for /f "tokens=1,* delims=:" %a in ('ipconfig^|findstr /n "^"^|findstr /l /b /c:"11:"') do echo %b
    

    执行命令,对输出进行编号,检索所需的行,拆分初始编号并回显其余部分

    set "x=1" & for /f "skip=10 delims=" %a in ('ipconfig') do @(if defined x (set "x=" & echo %a))
    

    设置标志变量,执行命令,跳过前 10 行,如果设置了标志,则每行清除标志并回显该行

    【讨论】:

    • 我可以在一个命令中运行它吗?我的意思不是通过批处理文件。
    • @ontherocks,我看不出有必要,但无论如何都包括在内。
    【解决方案2】:

    Could I run this in a single command? I mean not through a batch file.是的:

    ipconfig |find "Default Gateway"
    

    (在命令行和批处理文件中运行)

    【讨论】:

    • 它应该在命令行和批处理脚本中都能正常工作。
    猜你喜欢
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多