Page 1 of 1

DOS search for file contents? Like Grep

Posted: 07 Dec 2022 07:00
by OM3
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.

Re: DOS search for file contents? Like Grep

Posted: 07 Dec 2022 08:30
by Joe Caverly
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

Re: DOS search for file contents? Like Grep

Posted: 10 Dec 2022 08:23
by jfl
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!