ontherocks
ontherocks

Reputation: 1999

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?

Upvotes: 3

Views: 23543

Answers (2)

Stephan
Stephan

Reputation: 56180

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)

Upvotes: 2

MC ND
MC ND

Reputation: 70933

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

Upvotes: 5

Related Questions