Computing.Net > Forums > Programming > Batch Help: findstr use in for /f

Batch Help: findstr use in for /f

Reply to Message Icon

Original Message
Name: jirtan
Date: December 7, 2007 at 20:06:01 Pacific
Subject: Batch Help: findstr use in for /f
OS: Windows XP Pro
CPU/Ram: Intel Core Duo, 1 GB
Model/Manufacturer: ASUS
Comment:

Hi,

I'm having a hell of a time trying to figure out why this isn't working out for me. I've reduced the program to it's simplest useful part, and I'd like to know what exactly I'm doing incorrectly. I've tried variations on some parts, also with no luck. Any help is appreciated.

---test.bat---
for /f "usebackq tokens=3" %%g in (`findstr /r /c:"this is [0-9]* magic" test.txt`) do (
echo %g
)

pause
---end test.bat---

---test.txt---
abra kadabra
this is 88874 magic
last line
---end test.txt---


Report Offensive Message For Removal


Response Number 1
Name: jirtan
Date: December 7, 2007 at 20:09:30 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

Hah! It's %%g.


Report Offensive Follow Up For Removal

Response Number 2
Name: jirtan
Date: December 7, 2007 at 21:50:02 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

Might as well make this post useful.

Let's say I don't know what "this " and "agic" are in the line "this is 88874 magic". How would I find the value of any number (88874 in this case) in this situation?


Report Offensive Follow Up For Removal

Response Number 3
Name: IVO
Date: December 8, 2007 at 07:12:12 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

Replace your For with the following one where the /c switch is omitted from the FindStr command

for /f "tokens=3" %%g in ('findstr /r "this is [0-9]* magic" test.txt`) do echo %%g

as /c and /r are opposite directives.


Report Offensive Follow Up For Removal

Response Number 4
Name: jirtan
Date: December 8, 2007 at 12:26:06 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

Thanks for the help, but that makes it return false positives or nothing at all. I used /c: to make it search the string in one block (no "or" function), and I used /r to enable the use of regular expressions.


Report Offensive Follow Up For Removal

Response Number 5
Name: IVO
Date: December 9, 2007 at 01:33:34 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

I tested the code I posted using your test file and it returned the correct result (i.e. 88874), so you have to specify better what is the target of your search and the context related.


Report Offensive Follow Up For Removal


Response Number 6
Name: jirtan
Date: December 9, 2007 at 20:51:40 Pacific
Subject: Batch Help: findstr use in for /f
Reply: (edit)

I tested out your code again (modified with usebackq, as it is not on by default on my machine), and it worked just fine.

When I tested it beforehand, I just tried the findstr part and expected it to print only the lines of the file that matched the search string, but it might've printed the entire thing instead.

This was probably due to the fact that usebackq was not in the code, so it returned every line that had at least one token from the string.

Sorry for my ignorance, and thanks for the tip. :) I'll update my code right-away.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch Help: findstr use in for /f

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge