findstr输出到文件中的问题

马卡兰德·库尔卡尼

我正在尝试遵循命令

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt

跟进

index.html:<img src="/icons/unknown.gif" alt="[   ]"> <a 
 href="MOD13Q1.A2018257.h25v06.006.2018282132046.hdf">
FINDSTR: Cannot open >temp.txt

它不会将输出保存到temp.txt等其他命令

dir * >list.txt

工作正常

MC ND

您发现了一个问题,原因是cmd解析器和可执行程序参数解析器之间的引用处理不同

虽然这似乎是正确的

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                        ^^                           escaped quote to findstr
            ^.............^ ^..........^             arguments to findstr
                                         ^           redirection operator

您的问题是,当cmd尝试解析该行(以创建命令的内部表示并确定是否需要重定向)时,因为cmd双引号是“转义”(再次关闭并再次打开)引号,所以引号看过

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
                         ^^ escaped quote
            ^ open          ^close     ^open

这意味着一切都被视为 findstr

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
^.....^                                               command
        ^........................................^    argument

转义的引号将重定向操作符隐藏到将cmd所有东西传递给findstr

内部findstr参数处理是不同的,它看到

findstr /RC:"h25v06.*hdf\"" "index.html" >temp.txt
            ^.............^ ^..........^ ^.......^    arguments to findstr

这意味着预期的重定向现在被视为要在其中搜索的文件。

一种简单的解决方案是仅更改重定向的位置

>temp.txt findstr /RC:"h25v06.*hdf\"" "index.html" 

但是这留下了另一个问题。如所引用的,如果要处理的文件名findstr包含空格或特殊字符,则该命令将失败,因为它们不在加引号的区域内。

因此,我们需要一种方法来分隔两个引号,而在findstr表达式中不包含不需要的字符,但要正确关闭每个引号区域

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt

^"被视为cmd引号区域(由前一个引号封闭)中的真实转义引号,因此^不会传递给findstr现在对于cmd引用的区域是

findstr /RC:"h25v06.*hdf\"^" "index.html" >temp.txt
            ^............^   ^..........^

有问题的引号是一个转义的序列,被当作另一个字符处理并findstr接收预期的参数

findstr /RC:"h25v06.*hdf\"" "index.html" 
            ^.............^ ^..........^

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章