首页 > 解决方案 > 解析 bpftrace 输出数据

问题描述

我正在尝试使用 ruby​​ 解析 bpftrace 文件的以下输出。我考虑过拆分'|',然后我需要从中获取值"[4, 8) 824"。需要将这两个值放入一个数组中。我也在考虑使用 trim 方法,但肯定有更好的方法 - 也许使用正则表达式。请给我一些关于如何进行的指导,好吗?

输入:[4, 8) 824 |@@@@ |

first_array = []
text=File.foreach('/.../test.txt').with_index do |line|
   puts "#{line}"
   values=line.split("|")

   first_array=values[0].split(" ")
   puts first_array

标签: ruby

解决方案


您不需要按'|'开箱即用的非数字拆分,拆分:

input = '[4, 8) 824 |@@@@ |'
input.split(/\D+/).reject(&:empty?).map(&:to_i)
#⇒ [4, 8, 824]

或者,正如 Cary 在评论中所建议的:

input.scan(/\d+/).map(&:to_i)
#⇒ [4, 8, 824]

推荐阅读