parsing

  • Thread starter Thread starter Québec
  • Start date Start date
Q

Québec

Hi gurus,

I have a line of code here that works so so but I would like it to write the
name of the file in wich it found the word.
It search trough all files in a directory.

type *.c| findstr assert >>Out.txt

Jean
 
Hi gurus,

I have a line of code here that works so so but I would like it to write the
name of the file in wich it found the word.
It search trough all files in a directory.

type *.c| findstr assert >>Out.txt

Jean
for /f "Tokens=*" %%f in ('dir *.c /b') do (
for /f "Tokens=*" %%t in ('type "%%f"^|findstr /L /I /N /C:"assert"') do (
@echo %%f %%t >>Out.txt
)
)



Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Hi gurus,

I have a line of code here that works so so but I would like it to write the
name of the file in wich it found the word.
It search trough all files in a directory.

type *.c| findstr assert >>Out.txt

Jean
Minor safety change:

for /f "Tokens=*" %%f in ('dir *.c /b /a-d /a') do (
for /f "Tokens=*" %%t in ('type "%%f"^|findstr /L /I /N /C:"assert"') do (
@echo %%f %%t >>Out.txt
)
)


Jerold Schulman
Windows: General MVP
JSI, Inc.
http://www.jsiinc.com
 
Québec said:
Hi gurus,

I have a line of code here that works so so but I would like it to write the
name of the file in wich it found the word.
It search trough all files in a directory.

type *.c| findstr assert >>Out.txt

I find that when using findstr directly (without piping another command into
it), and when the file parameter includes a wildcard, the command will tell
me which file the match comes from.

findstr assert *.c >>Out.txt

This will produce a text file with a series of lines of this format:

<filename>:<line containing match>

If you want to list the files that match without actually listing the
matching line itself, use this:

for /f "delims=:" %%i in ('findstr assert *.c') do (echo %%i >>Out.txt)

If you want to run this command directly on the command line and not in a
script, change %%i to %i in both instances.

David
Stardate 4577.2
 
David Trimboli said:
I find that when using findstr directly (without piping another command into
it), and when the file parameter includes a wildcard, the command will tell
me which file the match comes from.

findstr assert *.c >>Out.txt

This will produce a text file with a series of lines of this format:

<filename>:<line containing match>
If you like to include the line number also:
findstr /N assert *.c >>Out.txt
giving
If you want to list the files that match without actually listing the
matching line itself, use this:

for /f "delims=:" %%i in ('findstr assert *.c') do (echo %%i >>Out.txt)
Or more easy:
findstr /M assert *.c >>Out.txt
 
David Trimboli said:
I find that when using findstr directly (without piping another command into
it), and when the file parameter includes a wildcard, the command will tell
me which file the match comes from.

findstr assert *.c >>Out.txt

This will produce a text file with a series of lines of this format:

<filename>:<line containing match>

Even good old find works well in this manner (for simple searches, at
least):

find /i "assert" *.c >>Out.txt

Try both, and pick the one whose output format you find most convenient.

/Al
 
Back
Top