Skip Multiple Lines in a Batch Script

Skip Multiple Lines in a Batch Script

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?

2 Answers

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

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)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.