如何解析ipconfig /all输出,仅提取Ethernet以太网适配器相关记录
Windows环境提取ipconfig /all中以太网适配器配置的方法
原生PowerShell实现(无需额外安装工具)
直接在PowerShell中执行以下命令即可输出目标内容:
# 存储ipconfig /all全量输出 $ipOutput = ipconfig /all # 定位首个Ethernet开头的行位置 $startLine = [array]::IndexOf($ipOutput, ($ipOutput -match '^Ethernet.*')[0]) # 定位首个Tunnel开头的行位置 $endLine = [array]::IndexOf($ipOutput, ($ipOutput -match '^Tunnel.*')[0]) # 输出中间所有内容 $ipOutput[$startLine..($endLine - 1)]
类Unix环境实现(Git Bash/WSL/Cygwin可用)
借助sed的范围匹配功能一行即可实现:ipconfig /all | sed -n '/^Ethernet/,/^Tunnel/p' | sed '$d'
其中sed '$d'作用是删除最后匹配到的Tunnel开头行,仅保留以太网适配器配置。
原生CMD实现
保存为.bat批处理文件直接运行即可:
@echo off setlocal enabledelayedexpansion set "extractFlag=0" for /f "delims=" %%l in ('ipconfig /all 2^>nul') do ( echo "%%l" | findstr /b /r /c:"Tunnel.*" >nul && set "extractFlag=0" if !extractFlag! equ 1 echo %%l echo "%%l" | findstr /b /r /c:"Ethernet.*" >nul && set "extractFlag=1" ) endlocal
内容的提问来源于stack exchange,提问作者Empty Coder
相关产品推荐
相关产品推荐

