findstr problem

G

Guest

findstr output has a problem when the search word happens to be on a last
line of the preceding file. The output will be garbled with the succeeding
file searched.
e.g here shows a sample command line session showing the problem:
test
findstr findstr
test
file1:findstrfile2:findstr


The desired output is one output per line like(this makes is easier to do
pipe operations) :
file1:findstr
file2:findst
 
P

Pegasus \(MVP\)

cliff said:
findstr output has a problem when the search word happens to be on a last
line of the preceding file. The output will be garbled with the succeeding
file searched.
e.g here shows a sample command line session showing the problem:
file1:findstrfile2:findstr


The desired output is one output per line like(this makes is easier to do
pipe operations) :
file1:findstr
file2:findst

It is not clear from your post how you were testing your claim.
Below is a sample batch file that attempts to duplicate what
you were doing:

@echo off
echo String1>file1
echo String2>>file1
echo String2>file2
echo String1>>file2
findstr "String2" file1 file2

When I run this batch file under WinXP Professional then I get
the following screen output, which is pretty much what I expected:

C:\Temp>try
file1:String2
file2:String2

What did you expect? What do you get when you run this batch file?
 
G

Guest

hello,

The last line should not have a newline for file1. When you used echo to
create file1, the last line has a newline.

Instead of using echo to create file1, try using notepad and make sure the
last line does not have a newline. I also noticed that "edit" also seems to
insert a newline for the last line(but not notepad).

I also tested grep(a sort of findstr equivalent) and it does not have a this
problem when given the same file1 and file2.

regards,
Cliff
 
P

Pegasus \(MVP\)

Now things are becoming clearer! All I can say is that
this is the way findstr.exe appears to be designed. If you're
interested in an in-depth discussion then I recommend that
you repost this item in alt.msdos.batch.nt. There are a few
players in there who will toss this one around until there
is no meat left on the bone (and probably not much of the
bone either!).
 
B

billious

Ah! - so (some of) the files involved may not have a terminal newline.

FINDSTR appears to reproduce exactly what it found in the file, where the
file is a series of records terminated by NEWLINE or (naturally) the
physical end-of-file; prefixed by filename: if it's dealing with multiple
files. Consequently, it appends filename2:file2line to filename1:file1line
without a newline if it's missing from filename1.

Try:
....
echo\>dummy.txt
findstr "whatever" file1.txt dummy.txt>temp1.txt
findstr "whatever" file2.txt dummy.txt>temp2.txt
copy t1.txt+dummy.txt+t2.txt+dummy.txt resultfile.txt /b

....and if blank lines are a problem,

for /f "delims=" %%i in (resultfile.txt) do echo %%i>newresult.txt

HTH

....Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top