为什么 FINDSTR 在我的批处理文件中找不到带有 space 的搜索字符串?
Why does FINDSTR not find the searched string with space in my batch file?
所以我运行一个FINDSTR命令在文件中搜索文本[errorCode: 0]
并使用选项/v
输出不包含此字符串的行。这是使用的代码:
@findstr /i /v "\[errorCode: 0\]" file1.log >>file2.txt
但是这会将file1.log
的所有内容放到file2.txt
。
我做错了什么?
使用以下命令行:
@%SystemRoot%\System32\findstr.exe /I /L /V /C:"[errorCode: 0]" file1.log >>file2.txt
由于 /L
和 /C
.
,这会显式运行对 [errorCode: 0]
的文字字符串搜索
关于 FINDSTR 的使用,其正则表达式支持与其他应用程序和解释器中的正则表达式支持无法比较,建议阅读 运行 findstr /?
在命令提示符 window 中另外还有:
- SS64 命令文档 FINDSTR。
- SS64 附加页 FINDSTR - Escapes and Length limits.
- What are the undocumented features and limitations of the Windows FINDSTR command? 由 Dave Benham 在 Stack Overflow 上撰写。
命令行
@findstr /i /v "\[errorCode: 0\]" file1.log >>file2.txt
没有产生预期的输出,因为双引号搜索字符串中的 space 字符被解释为多个搜索字符串之间的分隔符。所以 FINDSTR 正在使用此命令行搜索包含字符串 [errorCode:
或字符串 0]
的行,使用 case-insensitive 正则表达式 find 而不是 for包含字符串 [errorCode: 0]
的行并输出不包含这两个字符串中任何一个的所有行。
这个命令行也可以写成:
Literal 而不是正则表达式 find 搜索 case-insensitive for [errorCode
OR 0]
并输出不包含这两个字符串的所有行:
@findstr /I /L /V "[errorCode 0]" file1.log
Literal 而不是正则表达式查找 case-insensitive 显式搜索 [errorCode
OR 0]
在一行中输出所有不包含这两个字符串的行:
@findstr /I /V /C:"[errorCode:" /C:"0]" file1.log
常规 表达式查找 case-insensitive 显式搜索 [errorCode
OR 0]
在一行中输出所有不包含这两个字符串的行:
@findstr /I /R /V /C:"\[errorCode:" /C:"0\]" file1.log
例如file1.log
包含:
Line 1: [errorCode: 0]
Line 2: [CodeError: 0]
Line 3: [errorCode: 1]
Line 4: [noErrCode: 1]
上面三个命令行的输出和有问题的初始命令行是:
Line 4: [noErrCode: 1]
但是这个例子的预期输出是:
Line 2: [CodeError: 0]
Line 3: [errorCode: 1]
Line 4: [noErrCode: 1]
这是 运行 以下命令行之一的输出:
@findstr /I /V /C:"[errorCode: 0]" file1.log
@findstr /I /V /C:"\[errorCode: 0\]" file1.log
@findstr /I /L /V /C:"[errorCode: 0]" file1.log
@findstr /I /L /V /C:"\[errorCode: 0\]" file1.log
@findstr /I /R /V /C:"\[errorCode: 0\]" file1.log
\
是文字和正则表达式字符串中的转义字符。但在文字字符串中,只有反斜杠 \
本身和双引号字符 "
必须使用反斜杠进行转义。在正则表达式字符串中,具有特殊正则表达式含义的字符左侧的反斜杠会导致将下一个字符解释为文字字符。
我对 FINDSTR 的使用建议:
- 指定一个或多个字符串 compare with always with
/C:"..."
(or with /C:...
) and never with just with "..."
to在始终被解释为 space 字符的搜索字符串中获取 space。
- 始终为 literal 指定
/L
或为 r 正则表达式查找指定 /R
。
那么查找结果总是符合大多数用户的预期,他们知道从其他应用程序或解释器中搜索字符串,但不知道 FINDSTR 解释字符串以在 double 中查找的特定规则报价。
所以我运行一个FINDSTR命令在文件中搜索文本[errorCode: 0]
并使用选项/v
输出不包含此字符串的行。这是使用的代码:
@findstr /i /v "\[errorCode: 0\]" file1.log >>file2.txt
但是这会将file1.log
的所有内容放到file2.txt
。
我做错了什么?
使用以下命令行:
@%SystemRoot%\System32\findstr.exe /I /L /V /C:"[errorCode: 0]" file1.log >>file2.txt
由于 /L
和 /C
.
[errorCode: 0]
的文字字符串搜索
关于 FINDSTR 的使用,其正则表达式支持与其他应用程序和解释器中的正则表达式支持无法比较,建议阅读 运行 findstr /?
在命令提示符 window 中另外还有:
- SS64 命令文档 FINDSTR。
- SS64 附加页 FINDSTR - Escapes and Length limits.
- What are the undocumented features and limitations of the Windows FINDSTR command? 由 Dave Benham 在 Stack Overflow 上撰写。
命令行
@findstr /i /v "\[errorCode: 0\]" file1.log >>file2.txt
没有产生预期的输出,因为双引号搜索字符串中的 space 字符被解释为多个搜索字符串之间的分隔符。所以 FINDSTR 正在使用此命令行搜索包含字符串 [errorCode:
或字符串 0]
的行,使用 case-insensitive 正则表达式 find 而不是 for包含字符串 [errorCode: 0]
的行并输出不包含这两个字符串中任何一个的所有行。
这个命令行也可以写成:
Literal 而不是正则表达式 find 搜索 case-insensitive for
[errorCode
OR0]
并输出不包含这两个字符串的所有行:@findstr /I /L /V "[errorCode 0]" file1.log
Literal 而不是正则表达式查找 case-insensitive 显式搜索
[errorCode
OR0]
在一行中输出所有不包含这两个字符串的行:@findstr /I /V /C:"[errorCode:" /C:"0]" file1.log
常规 表达式查找 case-insensitive 显式搜索
[errorCode
OR0]
在一行中输出所有不包含这两个字符串的行:@findstr /I /R /V /C:"\[errorCode:" /C:"0\]" file1.log
例如file1.log
包含:
Line 1: [errorCode: 0]
Line 2: [CodeError: 0]
Line 3: [errorCode: 1]
Line 4: [noErrCode: 1]
上面三个命令行的输出和有问题的初始命令行是:
Line 4: [noErrCode: 1]
但是这个例子的预期输出是:
Line 2: [CodeError: 0]
Line 3: [errorCode: 1]
Line 4: [noErrCode: 1]
这是 运行 以下命令行之一的输出:
@findstr /I /V /C:"[errorCode: 0]" file1.log
@findstr /I /V /C:"\[errorCode: 0\]" file1.log
@findstr /I /L /V /C:"[errorCode: 0]" file1.log
@findstr /I /L /V /C:"\[errorCode: 0\]" file1.log
@findstr /I /R /V /C:"\[errorCode: 0\]" file1.log
\
是文字和正则表达式字符串中的转义字符。但在文字字符串中,只有反斜杠 \
本身和双引号字符 "
必须使用反斜杠进行转义。在正则表达式字符串中,具有特殊正则表达式含义的字符左侧的反斜杠会导致将下一个字符解释为文字字符。
我对 FINDSTR 的使用建议:
- 指定一个或多个字符串 compare with always with
/C:"..."
(or with/C:...
) and never with just with"..."
to在始终被解释为 space 字符的搜索字符串中获取 space。 - 始终为 literal 指定
/L
或为 r 正则表达式查找指定/R
。
那么查找结果总是符合大多数用户的预期,他们知道从其他应用程序或解释器中搜索字符串,但不知道 FINDSTR 解释字符串以在 double 中查找的特定规则报价。