2013-08-19 43 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 
}