zach
zach

Reputation: 71

Remove whitespace/trailing

I'm executing the following command to write the current IPv4 address to a logfile:

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

However, there are some whitespaces, how can I remove those?:

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

Thanks.

Upvotes: 1

Views: 288

Answers (4)

Magoo
Magoo

Reputation: 80023

@ECHO OFF
SETLOCAL

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

GOTO :EOF

Naturally, the echo could be a set if you so desire. The ++ either end of the data is merely to show that the spaces have been removed.

Find the target line(s) from an ipconfig command, select that part beyond the : on those lines, and process the resultant string, removing ant spaces (default delimiters in the for /f %%c)

Upvotes: 1

Bill_Stewart
Bill_Stewart

Reputation: 24535

PowerShell:

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

Upvotes: 0

ZGFubnk
ZGFubnk

Reputation: 500

This is a nice command you could use: http://fart-it.sourceforge.net/ You would use it to find and replace the part that is waste for your purpose.

Edit If you want stick with the commands available, then i think you would best use this: How to replace substrings in windows batch file

Look at Andriy M's first answer.

Upvotes: 0

HavelTheGreat
HavelTheGreat

Reputation: 3386

Maybe use ECHO and FOR to try something like this:

        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"
        )

Upvotes: 0

Related Questions