-1

Given a list of readings in the following format:

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

What would be an approriate (and clean !) way to map them to their labels (the labels are not in a fixed format, I just know from the documentation that column 1 maps to X, column 2 maps to Y etc. etc.).

The output is going into a text file for prometheus to read, so the desired format would be a two line format as follows:

# TYPE label_goes_here counter
label_goes_here value_goes_here

Ideally I would like this to use sh or ksh native tools. perl is ok too as its widely installed as default, unlike, say, python.

Edit to add example:

Given:

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

We could expect the output to look like:

# 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
  • Can you give a specific example of how you would like the output to look, using the data you provided at the top? – bitinerant Sep 21 '19 at 16:34
  • @bitinerant have edited quesiton with the example – Little Code Sep 21 '19 at 16:52
  • You can use awk to add the labels, e.g. echo "1 1 0 0 2 3 23101 23101 0 0 2 0 5 2 0 0" |awk '{print "name_of_label_1 "$1"\nname_of_label_2 "$2"\nname_of_label_3 "$3"\nname_of_label_4 "$4"\nname_of_label_5 "$5"\n"}', but I don't get the significance of the comments or counter. – bitinerant Sep 21 '19 at 17:03
  • @bitinerant I was rather hoping for something a little cleaner than hardcoding ? Obviously I can do that myself already, I was hoping to have a seperate list of labels and merge the two ? – Little Code Sep 21 '19 at 18:11
  • If you mean labels in a text file, can you add an example to the question? That part was not clear to me. – bitinerant Sep 21 '19 at 18:25
  • This seems to be possible in awk but that's not my tool of choice. – bitinerant Sep 21 '19 at 18:35
  • I didn't provide an example of labels because as mentioned in the question, I know what they are (from the docs) but the format of the list of labels can be whatever you like (array, text file etc. etc.). What would be your tool of choice ? I can expand the question to allow a Perl answer because Perl is pretty much everywhere. But no to something like Python because its not on BSD as standard (unlike Perl). – Little Code Sep 21 '19 at 18:48
  • You don't want to do this in sh or ksh. See Why is using a shell loop to process text considered bad practice?. awk is your best choice if you want a portable script - it's almost certain to be on every unix-like system in existence. – cas Sep 22 '19 at 03:33

3 Answers3

1

Let me know if this does what you need.

  • the labels.txt file used for testing:
label_zero
label_one
label_two
label_three
label_four
  • the Perl script to apply a label to each column (until they are depleted):
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])
           };'
  • the output:
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
cas
  • 78,579
  • this would be better if it took two filename args. the first being the labels file and the second being the data file. also, why not use while(<>)? you don't need to read the entire input file into an array, you can process it line by line. – cas Sep 22 '19 at 03:08
1

With 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
-1

Done by below command

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}'

output

# 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