Is there a split
command in Windows, to split command output? My command is:
ipconfig | findstr /i \"Default gateway\" | findstr [0-9]
>
On my computer, there are two gateways returned, one for IPv4 and one for IPv6. The findstr
doesn't distinguish between them. However, for me IPv4 is returned before IPv6. This batch file extracts the IP address for the gateway from the findstr
output:
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2 delims=:" %%i in ('ipconfig ^| findstr "Default gateway"') do (
if not defined _ip set _ip=%%i
)
for /f "tokens=1 delims= " %%i in ("!_ip!") do (
set _ip=%%i
)
echo !_ip!
endlocal & set yourvar=%_ip%
I split it into two separate for
commands (instead of nesting the for
commands) so that I could just grab the first 'gateway' returned from the findstr
. The result has a leading space, so the 2nd for
command removes the leading space. The & set yourvar=%_ip%
at the end is how you pass a local
variable outside of the local
block, but you don't have to use that...