我在 Windows 中有这个命令行输入:
dir /b | ruby -ne 'f=$_.chomp;File.read(f).each_line{|line| print f if line =~ /helloworld/};'
这一行是查找当前目录下所有包含helloworld
的文件。 (我知道还有很多其他方法可以做到这一点,但这篇文章并不关注这一点)
我的问题是:
此行给出错误提示 line is not a valid command
。然后我意识到 |
在 Ruby 中作为 block 变量的大括号是 cmd 中的管道符号。我试图将整行更改为双引号并转义 |签名但没有成功。如何转义竖线 |?
请不要回答其他方法来完成任务,例如使用其他工具,如 grep、find、sed 或 awk,或将 block 更改为 for 循环。
最佳答案
我找到了 this microsoft.com page关于管道和特殊字符的说明如下:
The ampersand (&), pipe (|), and parentheses ( ) are special characters that must be preceded by the escape character (^) or quotation marks when you pass them as arguments.
关于ruby - 使用 Ruby 执行命令行时如何转义竖线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12644837/