Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping to findstr's input

Tags:

I have a text file with a list of macro names (one per line). My final goal is to get a print of how many times the macro's name appears in the files of the current directory.

The macro's names are in C:\temp\macros.txt.

type C:\temp\macros.txt in the command prompt prints the list alright.

Now I want to pipe that output to the standard input of findstr.

type C:\temp\macros.txt | findstr *.ss (ss is the file type where I am looking for the macro names).

This does not seem to work, I get no result (very fast, it does not seem to try at all). findstr <the first row of the macro list> *.ss does work.

I also tried findstr *.ss < c:\temp\macros.txt with no success.

like image 912
Gauthier Avatar asked Jun 17 '10 13:06

Gauthier


People also ask

How use findstr command line?

You'll need to use spaces to separate multiple search strings unless the argument is prefixed with /C. For example, 'FINDSTR "hello there" x.y' searches for "hello" or "there" in file x.y. 'FINDSTR /C:"hello there" x.y' searches for "hello there" in file x.y.

What does the findstr command do?

In computing, findstr is a command in the command-line interpreters (shells) of Microsoft Windows and ReactOS. It is used to search for a specific text string in computer files.

How do you filter command output?

You can use the | { begin | exclude | include } regular-expression option to filter the display command output.

What is findstr in batch file?

Using the findstr command lets you search for text within any plaintext file. Using this command within a batch file lets you search for text and create events off the results found.


1 Answers

I think you're confusing a little how findstr works. It gets input (to find things in – not the things to look for) either as a file name (pattern) or from stdin, but the things you're looking for are always given on the command line as an argument to findstr.

findstr foo xyz.txt

finds the string foo in the file xyz.txt.

type meh.txt | findstr x

finds the string x in the output of the previous command (in this case the contents of the file meh.txt – a nice waste of the type command, much akin to common misuse of cat).

Since you're after the counts instead of the actual lines the macro names appear in, I'd suggest a different approach. This assumes that your file containing the macros lists them one per line:

for /f "delims=" %x in (macros.txt) do @(echo %x: & find /c "%x" *.ss)

The for loop iterates over the contents of your file, line-wise. It then proceeds to print the name you're searching for and executing find /c which actually counts matching lines.

like image 134
Joey Avatar answered Dec 19 '22 09:12

Joey