用标签映射读数

用标签映射读数

给出以下格式的读数列表:

1 1 0 0 2 3 23101 23101 0 0 2 0 5 2 0 0
1 1 0 0 2 2 23104 23104 0 0 1 0 5 1 0 0

将它们映射到标签的合适(并且干净!)的方法是什么(标签不是固定格式,我只是从文档中知道第 1 列映射到 X,第 2 列映射到 Y 等等) 。

输出将进入文本文件以供prometheus读取,因此所需的格式将是两行格式,如下所示:

# TYPE label_goes_here counter
label_goes_here value_goes_here

理想情况下,我希望使用此工具shksh本机工具。 perl也没关系,因为它被广泛安装为默认值,不像,比如说,python.

编辑以添加示例:

鉴于:

1 1 0 0 2 3 23101 23101 0 0 2 0 5 2 0 0

我们预计输出如下:

# TYPE name_of_label_1 counter
name_of_label_1 1
# TYPE name_of_label_2 counter
name_of_label_2 1
# TYPE name_of_label_3 counter
name_of_label_3 0
etc.
etc.
# TYPE name_of_label_16 counter
name_of_label_16 0

答案1

awk

LABELS='name_of_label_1 name_of_label_2 ...' awk '
  BEGIN{split(ENVIRON["LABELS"], label)}
  {
    for (i = 1; i <= NF; i++)
      print "# TYPE", label[i], "counter\n"label[i], $i
  }' < input-file

答案2

让我知道这是否满足您的需要。

  • labels.txt用于测试的文件:
label_zero
label_one
label_two
label_three
label_four
  • Perl 脚本将标签应用于每一列(直到它们耗尽):
echo "1 1 0 0 2 3 23101 23101 0 0 2 0 5 2 0 0" |
  perl -e 'open($lf, "<", "labels.txt");
           @lbs = <$lf>;
           chomp(@lbs);
           @cols = split(/ /, <STDIN>);
           for $i (0..$#cols) {
             printf("%s %s\n", $lbs[$i] || "label_".$i, $cols[$i])
           };'
  • 输出:
label_zero 1
label_one 1
label_two 0
label_three 0
label_four 2
label_5 3
label_6 23101
label_7 23101
label_8 0
label_9 0
label_10 2
label_11 0
label_12 5
label_13 2
label_14 0
label_15 0

答案3

通过以下命令完成

echo "1 1 0 0 2 3 23101 23101 0 0 2 0 5 2 0 0"| sed "s/ /\n/g"| awk '{print "# TYPE name_of_label_1 counter"NR}{print "name_of_label_"NR " " $0}'

输出

# TYPE name_of_label_1 counter1
name_of_label_1 1
# TYPE name_of_label_1 counter2
name_of_label_2 1
# TYPE name_of_label_1 counter3
name_of_label_3 0
# TYPE name_of_label_1 counter4
name_of_label_4 0
# TYPE name_of_label_1 counter5
name_of_label_5 2
# TYPE name_of_label_1 counter6
name_of_label_6 3
# TYPE name_of_label_1 counter7
name_of_label_7 23101
# TYPE name_of_label_1 counter8
name_of_label_8 23101
# TYPE name_of_label_1 counter9
name_of_label_9 0
# TYPE name_of_label_1 counter10
name_of_label_10 0
# TYPE name_of_label_1 counter11
name_of_label_11 2
# TYPE name_of_label_1 counter12
name_of_label_12 0
# TYPE name_of_label_1 counter13
name_of_label_13 5
# TYPE name_of_label_1 counter14
name_of_label_14 2
# TYPE name_of_label_1 counter15
name_of_label_15 0
# TYPE name_of_label_1 counter16
name_of_label_16 0

相关内容