Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
ontherocks Avatar asked Jun 21 '26 22:06

ontherocks


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

like image 51
MC ND Avatar answered Jun 24 '26 11:06

MC ND


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)

like image 29
Stephan Avatar answered Jun 24 '26 12:06

Stephan