移除 whitespace/trailing

Remove whitespace/trailing

我正在执行以下命令将当前 IPv4 地址写入日志文件:

ipconfig | findstr IPv4>>"c:\ip.log"

但是,有一些空格,我该如何删除它们?:

   IPv4 Address. . . . . . . . . . . : 10.0.0.15
   IPv4 Address. . . . . . . . . . . : 172.16.0.15
   IPv4 Address. . . . . . . . . . . : 192.168.0.15

谢谢。

这是一个很好的命令,您可以使用:http://fart-it.sourceforge.net/ 您可以使用它来查找和替换为您的目的而浪费的部分。

编辑 如果你想坚持使用可用的命令,那么我认为你最好使用这个:How to replace substrings in windows batch file

看Andriy M的第一个回答

也许可以使用 ECHOFOR 来尝试这样的事情:

        ipconfig | findstr IPv4>>"c:\ip.log"
        < "c:\ip.log" SET /P my_first_ip=
        FOR /F "delims= tokens=2" %%I IN ("%my_first_ip%") DO (
        ECHO %%I>"c:\ip.log"
        )

PowerShell:

get-wmiobject Win32_NetworkAdapterConfiguration -filter 'IPEnabled=TRUE' |
  select-object -expandproperty IPAddress | out-file "C:\IP.log" -append
@ECHO OFF
SETLOCAL

FOR /f "tokens=1*delims=:" %%a IN ('ipconfig^|findstr "IPv4" ') DO (
 FOR /f %%c IN ("%%b") DO ECHO ++%%c++
)

GOTO :EOF

当然,如果您愿意,echo 可以是 set++ 数据的任何一端只是为了表明空格已被删除。

ipconfig 命令中找到目标行,select 这些行上 : 之外的部分,并处理结果字符串,删除 ant 空格(默认for /f %%c)

中的分隔符