使用Ruby做命令行时,如何摆脱竖线? [英] How can I escape the vertical bar when doing command line with Ruby?

查看:72
本文介绍了使用Ruby做命令行时,如何摆脱竖线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows中有以下命令行输入:

I have this command line input in Windows:

dir /b | ruby -ne 'f=$_.chomp;File.read(f).each_line{|line| print f if line =~ /helloworld/};'

此行用于查找当前目录下的所有文件其中包含 helloworld 的目录。 (我知道还有很多其他方法可以做到这一点,但该帖子并不专注于此)。

This line is to find all files under current directory with helloworld in it. (I understand there are many other ways to do this but the post is not focusing on that)

我的问题是:

此行出现错误,指出行不是有效命令。然后我意识到在Ruby中 | 作为块变量的括号是cmd中的管道符号。我试图将整行更改为双引号并转义|。标志,但没有成功。我该如何逃避竖线|?

This line gives error saying line is not a valid command. Then I realized | in Ruby as a brace for block variable is a pipe sign in cmd. I tried to change the whole line into double quoted and escape the | sign but no success. How can I escape the vertical bar |?

请不要回答其他方式来执行任务,例如使用grep,find,sed或其他工具

推荐答案

我发现此microsoft.com页面说以下内容管道和特殊字符:

I found this microsoft.com page that says the following about pipes and special characters:


与号(&),管道(|)和括号()是特殊的
字符在将它们作为参数传递时,必须在其前面加上转义字符(^)或
引号。

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做命令行时,如何摆脱竖线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆