在cmd命令行上使用引号和其他特殊字符

时间:2015-02-24 13:53:43

标签: cmd escaping

我经常使用命令行工具,并且不时地使用解释器解释参数边界,然后使用引号。

示例:

grep -r "#include"  . | perl -pe "s/.*?#include.*?[\"<](.*?)[\">].*/\1/"

我想从子目录中的所有文件中提取所有包含的列表。问题似乎是cmd只接受双引号。因为它们也是正则表达式的一部分,我必须逃避它们。但这似乎不起作用。 inpterpreter抱怨无效的文件名方案(因为它将角括号错误解释为输入重定向操作符)。使用反斜杠似乎不起作用。

有没有人为我提供一些建议/提示/帮助如何解决这个问题? (旁边安装替代壳)

提前致谢。

1 个答案:

答案 0 :(得分:0)

修改即可。下一个命令行示例可以启发它(echo返回您在问题中提供的相同字符串):

==>echo "s/.*?#include.*?[\"^<](.*?)[\">].*/\1/"
"s/.*?#include.*?[\"<](.*?)[\">].*/\1/"
  亲爱的朋友,所有的理论都是灰色的,但生命的金色树木却是绿色的。 ( Johann Wolfgang von Goethe

"inner ^"double^" quotes" 应该导致内部“双重”引号(但不要)。例如,在作为参数提供的普通"s/.*?#include.*?[\"<](.*?)[\">].*/\1/"中,解析了三个“标记”:

  • "s/.*?#include.*?[\"
  • <](.*?)[\
  • ">].*/\1/"

下一个命令<在中间“令牌”(未被双引号括起)中转义 应该在批处理脚本和裸命令行中按预期工作。

perl -pe "s/.*?#include.*?[\"^<](.*?)[\">].*/\1/"

阅读Syntax : Escape Characters, Delimiters and Quotes。在下一批(.bat)脚本中,您可以看到一些示例:

@setlocal
@Echo ^@  - At Symbol: be less verbose
@Echo ^~  - Tilde: Parameter Expansion as in Call subroutines, FOR loops etc.
@Echo ^&  - Single Ampersand: used as a command separator
@Echo ^&^& - Double Ampersand: conditional command separator (if errorlevel 0)
@Echo ^|^| - Double Pipe: conditional command separator (if errorlevel ^> 0)
@Echo ^:^: - Double Colon: alternative to "rem" for comments outside of code blocks
@Echo ^^  - Caret: general escape character in batch
@Echo ^"  - Double Quote: surrounding a string in double quotes 
@Echo      escapes all of the characters contained within it
@Echo ^() - Parentheses: used to make "code blocks" of grouped commands
@Echo %%  - Percentage Sign: are used to mark three of the four variable types
@setlocal enabledelayedexpansion
@Echo ^^!  - Exclamation Mark: to mark delayed expansion environment variables ^^!var^^!
@endlocal
@setlocal disabledelayedexpansion
@Echo ^!  - Exclamation Mark: to mark delayed expansion environment variables ^!var^!
@endlocal
@Echo ^*  - Asterisk: wildcard matches any number or any characters
@Echo ^?  - Question Mark: matches any single character
@Echo ^.  - Single dot: represents the current directory
@Echo ^.. - Double dot: represents the parent directory of the current directory
@Echo ^\  - Backslash: represent the root directory of a drive dir ^\
@Echo ^|  - Single Pipe: redirects the std.output of one command
@Echo      into the std.input of another
@Echo ^NUL (File like device): is like a bottomless pit
@Echo ^CON (File like device): is a file like device that represents the console
@Echo ^>  - Single Greater Than: redirects output to either a file or file like device
@Echo ^>^> - Double Greater than: output will be added to the very end of the file
@Echo ^<  - Less Than: redirect the contents of a file to the std.input of a command
@Echo      Stream redirection: regarding the less and greater than symbols
@Echo      "http://judago.webs.com/batchoperators.htm"
@endlocal
@rem pause
@GOTO :eof