从简单的文本表中提取列数据

时间:2013-08-19 09:33:43

标签: ruby string parsing

我得到了这样的标准。

Queues
  queue                                          dur  autoDel  excl  msg   msgIn  msgOut  bytes  bytesIn  bytesOut  cons  bind
  ==============================================================================================================================
  14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0            Y        Y        0     0      0       0      0        0         1     2
  qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13       Y        Y        0     0      0       0      0        0         1     4

所以我需要解析这个输出并得到类似这样的东西

14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13

有谁能告诉我怎么做这个红宝石?当然可能有更多的线路。

2 个答案:

答案 0 :(得分:0)

按换行符分隔行(\n)。得到最后两行。

output = <<EOD
Queues
  queue                                          dur  autoDel  excl  msg   msgIn  msgOut  bytes  bytesIn  bytesOut  cons  bind
  ==============================================================================================================================
  14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0            Y        Y        0     0      0       0      0        0         1     2
  qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13       Y        Y        0     0      0       0      0        0         1     4
EOD

lines = output.strip.split("\n") # Split lines by newline
last_two_lines = lines[-2..-1] # Get the last 2 lines.
p last_two_lines.map {|line| line.split[0]} # Get the first fields.

打印

["14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0", "qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13"]

答案 1 :(得分:0)

queues = <<EOS
queue                                          dur  autoDel  excl  msg   msgIn  msgOut  bytes  bytesIn  bytesOut  cons  bind
==============================================================================================================================
14531c8d-dd9b-4f41-9d92-c1344774d21c:0.0            Y        Y        0     0      0       0      0        0         1     2
qmfagent-425fa29c-0892-4c08-a2d9-e7331a37dc13       Y        Y        0     0      0       0      0        0         1     4
EOS

queues.lines.each {|line|
  puts line.split.first if line =~ /[[\da-f]]{4}/i    # detects 4 consecutive hexadecimals
}