Skip multiple lines in a batch script

17,975

Solution 1

Just leave the loop

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

edited Get the 11th line in the output of a command from a single command line

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

Execute the command, number the output, retrieve the required line, split the initial number and echo the rest

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

Set a flag variable, execute the command, skip the first 10 lines and for each line if the flag is set, clean the flag and echo the line

Solution 2

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

ipconfig |find "Default Gateway"

(runs both on the command line and in a batch file)

Share:
17,975

Related videos on Youtube

ontherocks
Author by

ontherocks

Updated on June 05, 2022

Comments

  • ontherocks
    ontherocks almost 2 years

    I want to print out only a certain line from the output of a command. Lets take the example of ipconfig command. This returns a lot of lines.

    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  . :
    

    I want to just print say the 11th line.

    I tried the following

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

    This skips only the first 10 lines and prints the rest of the lines.

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

    How do I print only the 11th line?

  • ontherocks
    ontherocks over 9 years
    Could I run this in a single command? I mean not through a batch file.
  • MC ND
    MC ND over 9 years
    @ontherocks, i don't see the need, but anyway included.
  • dbenham
    dbenham over 9 years
    It should work fine both on the command line, and within a batch script.