Многострочный аргумент команды DOS команды findstr

41932
Arun
findstr /v "black" File1.txt 

Над командой DOS будет отображаться содержимое файла «File1.txt», который не соответствует строке «черный».

Как изменить эту команду, если мне нужно отфильтровать слова «черный» и «белый»?

16
Инструмент `findstr` не является частью MS-DOS. Поставляется с Windows (XP +?). Я думаю, что вы имеете в виду «инструмент командной строки» вместо «команда DOS». Michel de Ruiter 7 лет назад 0

2 ответа на вопрос

21
DavidPostill

How do I filter words "black" and "white"?

The following command will display all lines containing "black" NOR "white":

findstr /v "black white" blackwhite.txt 

The following command will display all lines containing "black" OR "white":

findstr "black white" blackwhite.txt 

The following command will display all lines containing EXACTLY "black white":

findstr /c:"black white" blackwhite.txt 

The following command will display all lines containing "black" AND "white":

findstr "white" blackwhite.txt | findstr "black" 

Notes:

  • When the search string contains multiple words, separated with spaces, then findstr will return lines that contain either word (OR).

  • A literal search (/C:string) will reverse this behaviour and allow searching for a phrase or sentence. A literal search also allow searching for punctuation characters.

Example data file (blackwhite.txt):

red black white blue black white black and white 

Example output:

F:\test>findstr /v "black white" blackwhite.txt red blue F:\test>findstr "black white" blackwhite.txt black white black white black and white F:\test>findstr /c:"black white" blackwhite.txt black white F:\test>findstr "white" blackwhite.txt | findstr "black" black white black and white 

Further Reading

очень интересно .. Я думаю, что это будет искать белый и черный `findstr" белый "File2.txt | findstr "черный" barlop 8 лет назад 1
хорошо, так как у нас есть NOR, так что есть еще перестановка, которую мы могли бы считать отсутствующей. NAND. Еще один, который мы могли бы считать отсутствующим, это XOR barlop 8 лет назад 0
@ barlop Я не могу понять, как сделать NAND или XOR: / Я знаю, каким должен быть результат **, но как это сделать ... DavidPostill 8 лет назад 0
может быть, нет хорошего быстрого способа, это, вероятно, будет пакетный файл, проверяющий уровень ошибок, возможно, лучше использовать какой-то другой инструмент, если он делает это, похоже, что grep не может. Но awk может делать совсем немного или, конечно, perl http://unix.stackexchange.com/questions/177513/grep-with-logic-operators barlop 8 лет назад 0
Давайте [продолжим это обсуждение в чате] (http://chat.stackexchange.com/rooms/25960/discussion-between-barlop-and-davidpostill). barlop 8 лет назад 0
0
Dean Spicer

If you need to display all lines with the words "black" or "white" then get rid of the /v in your command.

Try: findstr white File1.txt or findstr black File1.txt or findstr "black white" File1.txt

The /V operand will print all lines that DO NOT contain your search string.

Type findstr /? for more info on how to use findstr.