如何将shell输出发送到ruby -e?

时间:2022-06-08 14:04:00

Say I was typing something in my terminal like:

比如说,我在我的终端输入一些东西,比如:

ls | grep phrase

and after doing so I realize I want to delete all these files.

在这样做之后,我意识到我想删除所有这些文件。

I want to use Ruby to do so, but can't quite figure out what to pass into it.

我想用Ruby来做这个,但不知道该怎么做。

ls | grep phrase | ruby -e "what do I put in here to go through each line by line?"

2 个解决方案

#1


10  

Use this as a starting point:

以此作为起点:

ls ~ | ruby -ne 'print $_ if $_[/^D/]'

Which returns:

返回:

Desktop
Documents
Downloads
Dropbox

The -n flag means "loop over all incoming lines" and stores them in the "default" variable $_. We don't see that variable used much, partly as a knee-jerk reaction to Perl's overuse of it, but it has its useful moments in Rubydom.

-n标志意味着“遍历所有传入的行”,并将它们存储在“缺省”变量$_中。我们并没有看到这个变量被大量使用,这在一定程度上是对Perl过度使用它的本能反应,但它在Rubydom中有它有用的时刻。

These are the commonly used flags:

这些是常用的标志:

-e 'command'    one line of script. Several -e's allowed. Omit [programfile]
-n              assume 'while gets(); ... end' loop around your script
-p              assume loop like -n but print line also like sed

#2


2  

ARGF will save your bacon.

ARGF可以保存你的熏肉。

ls | grep phrase | ruby -e "ARGF.read.each_line { |file| puts file }"
=> phrase_file
   file_phrase
   stuff_in_front_of_phrase
   phrase_stuff_behind

ARGF is an array that stores whatever you passed into your (in this case command-line) script. You can read more about ARGF here:

ARGF是一个数组,它存储您传入的(在本例中为命令行)脚本的任何内容。你可以在这里读到更多关于ARGF的内容:

http://www.ruby-doc.org/core-1.9.3/ARGF.html

http://www.ruby-doc.org/core-1.9.3/ARGF.html

For more uses check out this talk on Ruby Forum: http://www.ruby-forum.com/topic/85528

更多的使用,请登录Ruby论坛:http://www.ruby-forum.com/topic/85528。

#1


10  

Use this as a starting point:

以此作为起点:

ls ~ | ruby -ne 'print $_ if $_[/^D/]'

Which returns:

返回:

Desktop
Documents
Downloads
Dropbox

The -n flag means "loop over all incoming lines" and stores them in the "default" variable $_. We don't see that variable used much, partly as a knee-jerk reaction to Perl's overuse of it, but it has its useful moments in Rubydom.

-n标志意味着“遍历所有传入的行”,并将它们存储在“缺省”变量$_中。我们并没有看到这个变量被大量使用,这在一定程度上是对Perl过度使用它的本能反应,但它在Rubydom中有它有用的时刻。

These are the commonly used flags:

这些是常用的标志:

-e 'command'    one line of script. Several -e's allowed. Omit [programfile]
-n              assume 'while gets(); ... end' loop around your script
-p              assume loop like -n but print line also like sed

#2


2  

ARGF will save your bacon.

ARGF可以保存你的熏肉。

ls | grep phrase | ruby -e "ARGF.read.each_line { |file| puts file }"
=> phrase_file
   file_phrase
   stuff_in_front_of_phrase
   phrase_stuff_behind

ARGF is an array that stores whatever you passed into your (in this case command-line) script. You can read more about ARGF here:

ARGF是一个数组,它存储您传入的(在本例中为命令行)脚本的任何内容。你可以在这里读到更多关于ARGF的内容:

http://www.ruby-doc.org/core-1.9.3/ARGF.html

http://www.ruby-doc.org/core-1.9.3/ARGF.html

For more uses check out this talk on Ruby Forum: http://www.ruby-forum.com/topic/85528

更多的使用,请登录Ruby论坛:http://www.ruby-forum.com/topic/85528。