[英]Head -1 in windows batch file
I'd like to get the first line only from the following command output in windows pe. 我只想从Windows pe中的以下命令输出获得第一行。 See below: 见下文:
wmic nic where "NetConnectionStatus=2" get netconnectionid |findstr /v "^Net"|findstr /v "^$"
Select -First would be an option, but it doesn't work. 选择-First是一个选项,但是不起作用。 I'm not familiar with windows too much I'm familiar with linux. 我对Windows不太熟悉,对Linux也不熟悉。
How could I get only the first line of the output. 我怎么能只获得输出的第一行。
If it's possible without findstr even better. 如果没有findstr可能更好。
How to get the first line is described here at SO . SO此处描述了如何获得第一行。 But you have to escape the ^
and the |
但是您必须避开^
和|
characters like it's said in this guide . 像本指南中所述的字符。
The following snipped will work: 下列内容将起作用:
@echo off
set "command=wmic nic where "netconnectionstatus=2" get netconnectionid ^| findstr /v "^^net" ^| findstr /v "^^$""
echo command: ^<%command%^>
for /f "delims=" %%a in ('%command%') do (
set tempvar=%%a
goto :print
)
:print
echo result: %tempvar%
I think the last pipe to findstr /v "^^$""
is unnecessary. 我认为findstr /v "^^$""
的最后一个管道是不必要的。
Finally a temporary file can help the best: 最后,一个临时文件可以最大程度地帮助您:
@echo off
wmic nic where "NetConnectionStatus=2" get netconnectionid |findstr /v "^Net"|findstr /v "^$" >tmp.tmp
set /p etc=<tmp.tmp
echo %etc%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.