DOS search for file contents? Like Grep

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

DOS search for file contents? Like Grep

#1 Post by OM3 » 07 Dec 2022 07:00

I need to search file contents. 99% of the time CSV files.

Windows explorer has a setting when searching that allows you to search file contents.
This is great. Except it doesn't always work.

I've used Grep in the past. That was a long time ago.
I don't actually want to install Grep.
But then just wondering if I could use a DOS command, similar to do what Grep does?

How efficient would it be?
In my folders, I'd have say 10,000 CSV files - all less than 10kb.

Not sure where to start and what commands to use.
I did a google and see suggestion for: findstr and find commands.

I tried find. I made a call like this:

Code: Select all

find "my string to search" *.*
That gave me some results that weren't helpful (in terms of display).
And I got this message: Access denied - on some folders. I opened CMD as admin and still got the same.

Hopefully I can get some pointers on what I should do.

Thanks.

Joe Caverly
Posts: 18
Joined: 11 Jul 2018 05:05

Re: DOS search for file contents? Like Grep

#2 Post by Joe Caverly » 07 Dec 2022 08:30

Not sure if this is a solution for you or not.

According to documentation at https://ss64.com/nt/setx.html
When using SetX to extract values from a file, we can ignore the variable that is set and instead use FOR /F to grab the extracted token text into the current session. In this way we are using the SetX command more like Findstr.

So to obtain the fourth token in the 1st line of example.txt:

Code: Select all

For /F "tokens=3" %%G in ('setx /F example.txt dummyVar /A 1^,4 ^|find "Extracted value"') do set _result=%%G
:: remove the trailing period
set _result=%result:~0,1%
Echo %_result%
I work with mostly .CSV files.

Here is how I would get the value from row 11, column 1, in my file covidtesting.csv;

Code: Select all

E:\Utils>setx Hospital /f covidtesting.csv /a 10,0 /d ","

Extracted value: 2020-02-15.

SUCCESS: Specified value was saved.

E:\Utils>echo %hospital
2020-02-15
Again, not sure if this is a solution for you, but just a suggestion.

Joe

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: DOS search for file contents? Like Grep

#3 Post by jfl » 10 Dec 2022 08:23

The ideal tool for that is ag.exe, aka. The Silver Searcher.
See the tool description in its home page on GitHub. (The first half describes the specificities of that Windows version; The second half describes ag features for both Unix and Windows.)
You can install it using winget:

Code: Select all

winget install "The Silver Searcher"
Or you can download the latest zip file, and install ag.exe anywhere you like in your PATH.

Then you search using a command like:

Code: Select all

ag "my string to search"
By default it searches for a regular expression in the current directory, and recursively in all subdirectories. There are many options for changing what is searched, selecting what files to search in, what results to display, etc. Run ag -? to display a list of all options.

ag.exe is much faster than find/grep ports for Windows.
I've used it with good results on servers with about 20GB of data, so searching in 100MB of CSV files should be reasonably fast. And a second search in the same data set will be surprisingly fast: ag.exe uses memory-mapped files. So it's they're in the cache already, the second search will be done entirely at RAM speed!

Post Reply