2013-08-19 22 views
0

我得到了这样的stdout。从简单的文本表中提取列数据

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 

任何人能告诉我如何做到这一点的红宝石?当然可以有更多的线条。

回答

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"] 
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 
}