tags:

views:

797

answers:

3

How to detect the following string from file safely with FIND (cmd.exe default commands), while the name minnie can be anything? its just that FROM: line has [email protected] on it.

From: "Minnie" <[email protected]>

it should not be mixed to this TO line :

To: <[email protected]>

e.g. this batch file row does not work properly :

find "[email protected]" abc.txt
A: 

I really don't think you're going to be able to accomplish that with find, since find only looks for a literal match and has no ability to use wildcards or regular expressions.

If you have the option, you can install the UnxUtils package and use grep to do it. It's a port of common Unix Utilities to Win32. You can find it at: [http://unxutils.sourceforge.net/][1]

You'd then issue a grep command like this:

grep "From.*me\@my\.com" abc.txt

Hope that helps!

JJ
does grep set errorlevel??
Tom
+1  A: 

Try two pipelined find commands, like this:

find "[email protected]" abc.txt | find "From:"

The former searches for all lines containing "[email protected]" and the latter filters them to leave only those lines that contain "From:".

Helen
Wow, Windows supports command pipelining? Learn something new every day! :)
JJ
+1  A: 

You can use findstr instead of find which has more advanced capabilities, like regular expression matching.

findstr /r /c:"^From:.*<[email protected]>" test.txt

will find the specified e-mail address only when the line starts with "From:".

findstr is also included by default at least since Windows 2000.

Joey