简体   繁体   English

了解Ruby stdout的工作原理

[英]Understanding how Ruby stdout works

i'm trying to understand how Ruby's stdout actually works, since i'm struggling with the output of some code. 我试图理解Ruby的stdout实际上是如何工作的,因为我正在努力处理某些代码的输出。 Actually, within my script i'm using a unix sort, which works fine from termina, but this is what i get from ruby, suppose you have this in your file (tsv) 实际上,在我的脚本中,我使用的是unix排序,从终端可以正常工作,但这是我从ruby得到的,假设你在你的文件中有这个(tsv)

a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m
a       b       c       d       e       f       g       h       i       l       m

My ruby code is this: 我的ruby代码是这样的:

@raw_file=File.open(ARGV[0],"r") unless File.open(ARGV[0],"r").nil?
tmp_raw=File.new("#{@pwd}/tmp_raw","w")
 `cut -f1,6,3,4,2,5,9,12 #{@raw_file.path} | sort -k1,1 -k8,8 > #{tmp_raw.path}`

This is what i get (misplaced separators): 这就是我得到的(错位的分隔符):

a       b       c       d       e       f       i
        1a       b       c       d       e       f       g       h       i       l       m
        1

Whats happening here? 这里发生了什么事? When running from terminal i get no separators misplacement 当从终端运行时,我没有分隔符错位

enter code here

Instead of writing to a temporary file, passing the file via argument etc, you can use Ruby's open3 module to create the pipeline in a more Ruby-friendly manner (instead of relying on the underlying shell): 您可以使用Ruby的open3模块以更加Ruby友好的方式创建管道(而不是依赖于底层shell),而不是写入临时文件,通过参数等传递文件:

require 'open3'

raw_file = File.open(ARGV[0], "r")

commands = [
  ["cut", "-f1,6,3,4,2,5,9,12"],
  ["sort", "-k1,1", "-k8,8"],
]

result = Open3.pipeline_r(*commands, in: raw_file) do |out|
  break out.read
end

puts result

Shell escaping problems, for example, become a thing from the past, and no temporary files are necessary, since pipes are used. 例如,Shell逃避问题成为过去的事情,因为管道被使用,所以不需要临时文件。

I would, however, advise doing this kind of processing in Ruby itself, instead of calling external utilities; 但是,我会建议在Ruby本身进行这种处理,而不是调用外部实用程序; you're getting no benefit from using Ruby here, you're just doing shell stuff. 你在这里使用Ruby没有任何好处,你只是做shell的东西。

As Linuxios says, your code never uses STDOUT, so your question doesn't make a lot of sense. 正如Linuxios所说,你的代码从不使用STDOUT,所以你的问题没有多大意义。

Here's a simple example showing how to do this all in Ruby. 这是一个简单的例子,展示了如何在Ruby中完成所有这些操作。

Starting with an input file called "test.txt": 从名为“test.txt”的输入文件开始:

    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m
    a       b       c       d       e       f       g       h       i       l       m

This code: 这段代码:

File.open('test_out.txt', 'w') do |test_out|
  File.foreach('test.txt') do |line_in|
    chars = line_in.split
    test_out.puts chars.values_at(0, 5, 2, 3, 1, 4, 8, 10).sort_by{ |*c| [c[0], c[7]] }.join("\t")
  end
end

Creates this output in 'test_out.txt': 在'test_out.txt'中创建此输出:

    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m
    a       b       c       d       e       f       i       m

Read about values_at and sort_by . 阅读values_atsort_by

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM