1

Trying to find line in a file but an having an issue with the literal search string. When search for a string it is returning multiple line that where the search string is only part of the line. As an example, I am looking up comp1 in a list of computers and i am getting comp1 and comp1a being returned. Is there a way to just return the first and not the second, or is this a limitation of findstr. Code below

For /f %%a in (%home%\text.txt) do call :look1 %%a

:look1
 set lookup=%1
 findstr /i /c:%lookup% %home%\data.csv >> %home%\final.csv
 exit /b

Edited to include sample of data.csv

Data.csv

date,comp,os,application

date,comp1,os,application

date,comp1a,os,application

date,comp2,os,application

1
  • Include the leading and trailing commas in your search term. Commented Jan 17, 2014 at 5:45

2 Answers 2

1

I'd suggest

FINDSTR /r "^comp, ,comp$ ,comp," <qfindrsl.txt

would be the way to go. This matches on any of the regex expressions "^comp," (comp, at the start of the line), ",comp," (or this string) or ",comp$" (,comp at end-of-line.)


Edit : I put the data in qfindrsl.txt. That worked both with my test data and with the sample data provided.

If the data will not contain the target string at either the start or end of lines, this could be simplified to

FINDSTR /r ",comp," <qfindrsl.txt
Sign up to request clarification or add additional context in comments.

2 Comments

I should probably have put that data.csv file has the computer name in a line with lots of other data and it is not the first or last thing in the line.
Representative sample data often clarifies matters. You can edit your question to include this.
0

You can use the regexp terms to limit the find filter:

^ indicates the start of a line
$ indicates the end of a line

 findstr /r "^comp1$" "filename.txt"

Another way is to use /b and /e in findstr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.