batch remove text from file?

I'm trying to write a windows batch file that will either extract or remove text from a file not matching a regex.

What I'm doing is this:

Run traceroute & copy results to file
Run findstr regex against results to extract only class a/b/c addresses

Now the problem is findstr is copying the whole line which is no good to me because I want to use the IP on the last line as a variable for the next piece of my script that will use that IP to try and download a log file from our equipment.

Anybody knows how I could achieve what I want?
 
Look into using powershell scripting to do this. It has support for real regex with capture groups and extraction. It can even do c# objects and libraries.
 
Powershell isn't installed by default on Windows XP as far as I known. The machines the script will be executed on aren't managed by me and there will undoubtedly be a fair amount of XP machines without the possibility of installing powershell or requesting powershell to be installed. Maybe I should consider making two versions though I'd rather stick with one.

Is there a lot of information available on scripting for powershell? I don't really know how to program and I'm mostly cobbling stuff together hehe.
 
You can definitely get FOR /F to do what you want. Also keep in mind that there are tons of guys out there on Stack Overflow and such that Albuquerque would like nothing better than to write a batch script for you.
 
Last edited:
Thanks. I did figure it out on my own today by using tokens and delims. The only weird thing is that even though my input file has multiple lines, the script automatically only processes the last one. I my case that is great as it's exactly what I need but it does make me wonder why.
 
I'm wizard level in batch script; post up your code and I can help :)

Edit: Also, that FOR /F link posted by Otto isn't correct in at least one metric: the "usebackq" function is to parse input directly from a stdout stream. As an example, pop this into a command line:

for /F "usebackq delims=*" %A in (`dir c:\ /a /b`) do @echo %A


... and you'll get the directory output of every file and folder on the root of your C:\ drive, even those with spaces. Here's the crux: the stdout readback isn't "live", you have to wait for the embedded command to complete before FOR begins executing. So if you swapped in a (`dir c:\ /s /a /b`) into the command above, it would seem to "pause" for many minutes (maybe even hours) as the DIR command reads every single file and folder on your entire harddrive. Wheeeeeeeeee *snore*
 
Last edited:
I'm wizard level in batch script; post up your code and I can help :)

Edit: Also, that FOR /F link posted by Otto isn't correct in at least one metric: the "usebackq" function is to parse input directly from a stdout stream. As an example, pop this into a command line:

for /F "usebackq delims=*" %A in (`dir c:\ /a /b`) do @echo %A


... and you'll get the directory output of every file and folder on the root of your C:\ drive, even those with spaces. Here's the crux: the stdout readback isn't "live", you have to wait for the embedded command to complete before FOR begins executing. So if you swapped in a (`dir c:\ /s /a /b`) into the command above, it would seem to "pause" for many minutes (maybe even hours) as the DIR command reads every single file and folder on your entire harddrive. Wheeeeeeeeee *snore*

Alright I edited my post :p
 
I'm wizard level in batch script; post up your code and I can help :)

Edit: Also, that FOR /F link posted by Otto isn't correct in at least one metric: the "usebackq" function is to parse input directly from a stdout stream. As an example, pop this into a command line:

for /F "usebackq delims=*" %A in (`dir c:\ /a /b`) do @echo %A


... and you'll get the directory output of every file and folder on the root of your C:\ drive, even those with spaces. Here's the crux: the stdout readback isn't "live", you have to wait for the embedded command to complete before FOR begins executing. So if you swapped in a (`dir c:\ /s /a /b`) into the command above, it would seem to "pause" for many minutes (maybe even hours) as the DIR command reads every single file and folder on your entire harddrive. Wheeeeeeeeee *snore*

This is the piece of code I was talking about.

Code:
tracert -d -h 10 8.8.8.8 > tracert.txt
findstr /G:pattern.txt tracert.txt > tracert_results.txt
for /f "tokens=8 delims= " %%A in (tracert_results.txt) do set TerminalIP=%TerminalIP% %%A

pattern.txt contains the regex to only extract A, B and C class IP addresses. The question I'm having is that even if my tracert results are returning multiple A, B or C addresses, for some reason the last IP is always set as the TerminalIP. Again this works perfect for my purposes but I don't know why it's happening.
 
This is the piece of code I was talking about.

Code:
tracert -d -h 10 8.8.8.8 > tracert.txt
findstr /G:pattern.txt tracert.txt > tracert_results.txt
for /f "tokens=8 delims= " %%A in (tracert_results.txt) do set TerminalIP=%TerminalIP% %%A

pattern.txt contains the regex to only extract A, B and C class IP addresses. The question I'm having is that even if my tracert results are returning multiple A, B or C addresses, for some reason the last IP is always set as the TerminalIP. Again this works perfect for my purposes but I don't know why it's happening.

Doesn't TerminalIP get set to every successive pattern match (for each iteration of the loop), and remain set on the last one when the loop exits?
 
Otto has it exactly right; yet it's not intuitive. While there is no explicit sense of Global vs Local variables in Microsoft command line logic, what's happening is the %TERMINALIP% environment variable is being treated as "local" during the iteration of the FOR loop. Thus, at the start of iteration in the FOR loop sequence, the variable is reset to the value it held before FOR started.

The net result is, only the very last line of the FOR result is maintained in the environment variable. It's a limitation of how the FOR command was written and how MS command interpreter handles environment variables.
 
Back
Top