0

I'm trying to make a find command find the line that says "1-Male;" in a file called "gender.txt" in the same directory. The problem is that the find command also echos a blank line and the directory of said gender.txt, and I want to know how do I get the find command to, you know, not say those two lines?

@echo off
color 0a
title Test #1
echo What gender do you want to be?
echo[
echo 1. Male
echo 2. Female
echo 3. Robot
echo[
choice /c 123 /n /m ">"
set index=%errorlevel%
find "%index%" "%cd%\gender.txt"
pause & exit /b

I should also probably say what's the contents in "gender.txt".

1-Male
2-Female
3-Robot
2
  • Use findstr instead of find
    – DavidPostill
    Commented Sep 14, 2020 at 17:20
  • If %cd% does not contain 1,2 or 3: find "%index%" "%cd%\gender.txt"|find "%index%" or another approach type "%cd%\gender.txt"|find "%index%".
    – JosefZ
    Commented Sep 14, 2020 at 17:59

1 Answer 1

0

As David Postill commented, you can do this using findstr instead of find

C> findstr 1-Male *.txt
foo.txt:1-Male

C> findstr /M 1-Male *.txt
foo.txt

C> findstr /N 1-Male *.txt
foo.txt:1:1-Male

C> findstr /N 1-Male foo.txt
1:1-Male

C> findstr 1-Male foo.txt
1-Male

I imagine at least one of those options delivers what you seek.

Bonus material : Why are there both FIND and FINDSTR programs, with unrelated feature sets? by Raymond Chen.

Basically findstr is what happens when a Microsoft engineer who is used to grep encounters find, is driven by exasperation to do better, but only implements 0.01% of grep.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .