高级文本搜索 windows 命令行实用程序
advanced text searching windows command line utility
是否存在 Windows 的命令行实用程序可以搜索比 FIND 和 FINDSTR 更复杂的文本?无论单词的顺序如何,都能找到包含指定关键字的所有文本行的东西?
例如,如果关键字是 "Happily Ever After",它应该找到包含 "Ever After Happily" 和 "If ever that happens after today, she will happily embrace it" 但不包含 "happily together".
的行
type search.txt | findstr /i /r "\<happily\>" | findstr /i /r "\<ever\>" | findstr /i /r "\<after\>"
我用了测试用例...
search.txt:
Happily Ever After
Happily Ever After blah
blah Happily Ever After
Happily Ever blah After
If ever that happens after today, she will happily embrace it
happily together
happily
ever
after
happily ever
ever after
after happily
every happily afterwards
这给出了输出:
Happily Ever After
Happily Ever After blah
blah Happily Ever After
Happily Ever blah After
If ever that happens after today, she will happily embrace it
仅以上。
这是您需要的吗?
我喜欢 ,因为它只使用本机命令。但是,它必须为要搜索的每个词读取一次文件。如果您添加大量额外的测试,这可能会减慢速度。
您可以使用我的 JREPL.BAT hybrid Jscript/batch regex utility,再加上一些用户提供的 JScript,并获得一个高效且可靠的解决方案,该解决方案通过单次遍历文本即可生成结果。 JREPL 是纯脚本,可以在从 XP 开始的任何 Windows 机器上本地运行。完整的文档嵌入在脚本中,可以通过 jrepl /?
或 jrepl /??
访问分页输出。
这是一种使用 JREPL 的可能解决方案。我将变量 a
、b
和 c
初始化为 0,然后在每一行中搜索所需的单词,如果找到,每个单词将不同的变量设置为 1。替换字符串还必须用自身替换找到的单词。在每一行的末尾,如果 a+b+c <> 3
之和,我禁用该行的打印输出,然后我将所有三个变量重置为 0 以准备下一行。最后的 /F
选项指定我正在搜索文件 "test.txt"。我使用续行来使长命令更易于阅读。
call jrepl "\b(?:(happily)|(ever)|(after))\b"^
"if () a=1; else if () b=1; else c=1; [=10=]"^
/jbeg "var a=0,b=0,c=0"^
/jendln "if (a+b+c!=3) $txt=false; a=b=c=0"^
/j /i /f test.txt
下面是另一个使用相同基本逻辑的解决方案,只是我使用 /T 选项稍微简化了代码。 /T 选项类似于 Oracle Translate() 函数、unix tr 命令或 sed y 命令。
call jrepl "\bhappily\b \bever\b \bafter\b"^
"a=1;[=11=] b=1;[=11=] c=1;[=11=]"^
/jbeg "var a=0,b=0,c=0"^
/jendln "if (a+b+c!=3) $txt=false; a=b=c=0"^
/j /i /t " " /f test.txt
是否存在 Windows 的命令行实用程序可以搜索比 FIND 和 FINDSTR 更复杂的文本?无论单词的顺序如何,都能找到包含指定关键字的所有文本行的东西?
例如,如果关键字是 "Happily Ever After",它应该找到包含 "Ever After Happily" 和 "If ever that happens after today, she will happily embrace it" 但不包含 "happily together".
的行type search.txt | findstr /i /r "\<happily\>" | findstr /i /r "\<ever\>" | findstr /i /r "\<after\>"
我用了测试用例...
search.txt:
Happily Ever After
Happily Ever After blah
blah Happily Ever After
Happily Ever blah After
If ever that happens after today, she will happily embrace it
happily together
happily
ever
after
happily ever
ever after
after happily
every happily afterwards
这给出了输出:
Happily Ever After
Happily Ever After blah
blah Happily Ever After
Happily Ever blah After
If ever that happens after today, she will happily embrace it
仅以上。
这是您需要的吗?
我喜欢
您可以使用我的 JREPL.BAT hybrid Jscript/batch regex utility,再加上一些用户提供的 JScript,并获得一个高效且可靠的解决方案,该解决方案通过单次遍历文本即可生成结果。 JREPL 是纯脚本,可以在从 XP 开始的任何 Windows 机器上本地运行。完整的文档嵌入在脚本中,可以通过 jrepl /?
或 jrepl /??
访问分页输出。
这是一种使用 JREPL 的可能解决方案。我将变量 a
、b
和 c
初始化为 0,然后在每一行中搜索所需的单词,如果找到,每个单词将不同的变量设置为 1。替换字符串还必须用自身替换找到的单词。在每一行的末尾,如果 a+b+c <> 3
之和,我禁用该行的打印输出,然后我将所有三个变量重置为 0 以准备下一行。最后的 /F
选项指定我正在搜索文件 "test.txt"。我使用续行来使长命令更易于阅读。
call jrepl "\b(?:(happily)|(ever)|(after))\b"^
"if () a=1; else if () b=1; else c=1; [=10=]"^
/jbeg "var a=0,b=0,c=0"^
/jendln "if (a+b+c!=3) $txt=false; a=b=c=0"^
/j /i /f test.txt
下面是另一个使用相同基本逻辑的解决方案,只是我使用 /T 选项稍微简化了代码。 /T 选项类似于 Oracle Translate() 函数、unix tr 命令或 sed y 命令。
call jrepl "\bhappily\b \bever\b \bafter\b"^
"a=1;[=11=] b=1;[=11=] c=1;[=11=]"^
/jbeg "var a=0,b=0,c=0"^
/jendln "if (a+b+c!=3) $txt=false; a=b=c=0"^
/j /i /t " " /f test.txt