在 Windows 中,find和findstr命令之间有什么区别?
两者似乎都在文件中搜索文本:
C:\> find /?
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
/V Displays all lines NOT containing the specified string.
/C Displays only the count of lines containing the string.
/N Displays line numbers with the displayed lines.
/I Ignores the case of characters when searching for the string.
/OFF[LINE] Do not skip files with offline attribute set.
"string" Specifies …Run Code Online (Sandbox Code Playgroud) findstr /v "black" File1.txt
Run Code Online (Sandbox Code Playgroud)
以上 DOS 命令将显示“File1.txt”中与字符串“black”不匹配的内容。
如果我需要过滤单词“black”和“white”,如何修改这个命令?
WindowsFINDSTR命令对于筛选大量数据很有用;它过滤掉匹配或不匹配指定模式或字符串(如 GNU/BSD grep)的文本行。
但它拒绝输出超过一定长度的行。是否有(本机)替代、修复或解决方法?
我试图让 cmd 只从命令输出中复制一组特定的字符。我已经能够选择该短语所在的行,但从那里我迷路了。到目前为止,这是我的命令。
ipconfig | findstr /i "ipv4"
Run Code Online (Sandbox Code Playgroud)
我相信你熟悉ipconfig命令和findstr命令。基本上我正在隔离特定计算机的 IPv4 地址。这就是上面的命令。
IPv4 Address. . . . . . . . . . . : 192.168.1.75
Run Code Online (Sandbox Code Playgroud)
我想隔离“192.168.1”。并将其保存到一个变量中,以便稍后在命令中使用。假设我设置了“192.168.1”。到变量a。
我希望运行的下一个命令扫描从 192.168.1.1 到 192.168.1.254 的所有 IP 地址,并保存正在使用的地址。由于这个,即将成为程序,将在不同位置的不同计算机上运行“192.168.1”。会有所不同,我正在寻找隔离任何 cmd 放在它的位置。所以上述变量a可以是但不限于192.168.10.、10.10.10.,甚至1.1.1。视情况而定。这是我将使用的代码。
FOR /L %i IN (1,1,254) DO ping -n 1 (the 'a' variable)%i | FIND /i "bytes=">>c:\ipaddresses.txt
Run Code Online (Sandbox Code Playgroud)
或者如果查看变量的外观有帮助。
FOR /L %i IN (1,1,254) DO ping -n 1 192.168.1.%i | FIND /i "bytes=">>c:\ipaddresses.txt
Run Code Online (Sandbox Code Playgroud)
我使用的是 64 位 Windows 10 pro …
这个批处理文件
@echo off
set path=C:\Users\ssiyengar\Desktop\Pingtest\pinglist.csv
set file=C:\Users\ssiyengar\Desktop\Pingtest\temp.txt
set qosping=1
cls
for /f "tokens=1-3 delims=," %%a IN (%path%) do (
ping %%c -n %qosping% > %file%
findstr "time< time=" %file% >nul
if %errorlevel%==1 (
echo %%a %%b IP %%c Ping FAILURE
) else (
echo %%a %%b IP %%c Ping SUCCESS
)
)
pause
Run Code Online (Sandbox Code Playgroud)
输出:
'ping' is not recognized as an internal or external command,
operable program or batch file.
'findstr' is not recognized as an internal or external command,
operable …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我一生都无法弄清楚在使用 vim 时如何将单引号传递给外部工具。例如,我可能想调用 Vim 的外部 grep 来搜索文本“foo”(即单引号然后是单词 foo)
所以我可能会尝试以下命令:
:grep \"foo *.txt
Run Code Online (Sandbox Code Playgroud)
但这会导致以下错误消息:
!findstr /n \"foo *.txt >C:\Users\[username]\AppData\Local\Temp\VIeC244.tmp 2>&1
shell returned 1
E40: Can't open errorfile C:\Users\[username]\AppData\Local\Temp\VIeC244.tmp
Run Code Online (Sandbox Code Playgroud)
请注意,grep 已更改为使用 'findstr /n',因为我在 Windows 上(并且可以通过运行来验证echo &grepprg)。我发现如果我自己从命令行输入以下内容,我可以轻松地执行我想要的搜索:
findstr /n \"foo *.txt
Run Code Online (Sandbox Code Playgroud)
这完全符合我的预期
我可以使用以下命令从 Windows 命令提示符中搜索包含单词“string”的文本文件:
C:\>findstr /spin /c:"string" *.txt
Run Code Online (Sandbox Code Playgroud)
如果我想搜索子目录怎么办?以下不起作用:
C:\>findstr /spin /c:\Users\My Name\Desktop"string" *.txt
Run Code Online (Sandbox Code Playgroud)
是否可以使用 findstr 搜索特定的子路径?我知道我可以执行以下操作,但我试图避免首先更改目录:
C:\Users\My Name\Desktop>C:\>findstr /spin /c:"string" *.txt
Run Code Online (Sandbox Code Playgroud) 我在编写软件时经常使用命令行程序“findstr”。它帮助我在一组子目录中快速定位包含特定字符串的所有文件。在很多情况下,使用“findstr”比其他任何东西都要快得多。今天我遇到了一个问题,我想找到字符串">,所以我运行了这个命令(我认为这是一个相当典型的转义字符串):
findstr /sic:"\">" *
Run Code Online (Sandbox Code Playgroud)
并得到这条神秘的错误消息:“文件名、目录名或卷标语法不正确。”
将其更改为:
findstr /sic:'\">' *
Run Code Online (Sandbox Code Playgroud)
工作正常。为什么我需要使用单引号而不是双引号?在我在双引号包装器中转义双引号之前,我已经运行了数百(也许数千?)个 findstr 命令,没有任何问题。是什么让这个特定的搜索字符串如此不同?
findstr ×8
command-line ×4
windows ×3
cmd.exe ×2
batch ×1
batch-file ×1
find ×1
grep ×1
ip-address ×1
ipconfig ×1
ping ×1
string ×1
vim ×1
windows-10 ×1
windows-7 ×1