Yash
Yash

Reputation: 173

How to avoid adding file names in 'findstr' results?

I am using findstr to search an string in some file and redirecting the search result to another txt file but need to omit the searched file name added in results.

My batch file has :- findstr /s "TEST" subfolder/test.dat > output.txt

and the result of output.txt with the filename test.dat(which I need to remove):-

subfolder/test.dat:2014-04-15;TEST TECHNOLOGY LTD

Same kind of question has been asked here. But in my case I am not using any wildcards. Please help.

Upvotes: 0

Views: 2037

Answers (2)

npocmaka
npocmaka

Reputation: 57252

One more way is to pipe with TYPE

If there's only one file:

type "subfolder/test.dat" |Findstr "TEST" > output.txt

The S switch is for searching in subfolders so if the files are more:

for /r "subfolder" %%a in (*.dat) do type %%~fa|Findstr "TEST" >> output.txt

Upvotes: 0

woxxom
woxxom

Reputation: 73526

  1. Parse the output of recursive dir:

    >output.txt (
        for /f "eol=* delims=" %%a in ('dir /s /b "subfolder\test.dat"') do (
            findstr /c:"TEST" "%%a"
        )
    )
    
    • eol=* is used to correctly parse folders with ; which is treated as comment by for /f
    • delims= (empty) means that linebreak is used to delimit the output, so the line is taken entirely
    • /c: is used to indicate literal search string so that it may contain spaces
  2. Alternatively you can strip the file names from the recursive findstr's output:

    >output.txt (
        for /f "delims=: tokens=1*" %%a in (
            'findstr /s /c:"TEST" "subfolder\test.dat"'
        ) do echo.%%b
    )
    

    In case you want to specify a drive qualifier use tokens=2*.

Upvotes: 1

Related Questions