如何让“find”命令在使用时不说出文件目录和名称?

如何让“find”命令在使用时不说出文件目录和名称?

我正在尝试让 find 命令在同一目录中名为“gender.txt”的文件中查找显示“1-Male;”的行。问题是 find 命令还会回显一个空白行和所述 gender.txt 的目录,我想知道如何让 find 命令不显示这两行?

@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

我可能还应该说一下“gender.txt”中的内容是什么。

1-Male
2-Female
3-Robot

答案1

findstr正如David Postill 所评论的那样,你可以使用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

我想这些选项中至少有一个可以满足您的要求。

奖励材料:为什么有 FIND 和 FINDSTR 程序,但它们的功能集却不相关?作者:Raymond Chen。

基本上findstr就是当一个习惯于遇到的微软工程师grepfind在愤怒的驱使下想要做得更好,但却只实现了 0.01% 时所发生的事情grep

相关内容