Reputation: 1300
I need to process output that looks as below:
my_first_key: {i}text
my_first_key: {j}different_text
my_first_key: {k}some_text
my_second_key: value1
my_first_key: {l}some_text
my_second_key: value2
Lines always begin with one of two keys followed by :
then a space then either an index in curly braces for one key, or immediately a value for the second key. A line with the second-type key and the value is always preceded by a line with first key and the index.
I need two functions:
array()
: to return the array as {k:value1, l:value2}
.index(value)
to return the index value from the preceding line so index(value2)
returns l
.I see examples to return line numbers etc., but my awk
level is 0.
[From Moshe comment] Algorithm is like this:
a) awk '/$value/{ print NR; exit }'
b) awk 'NR==$(previous_return - 1)
c) awk '{split($0,a,"{}") | print a[2]
Upvotes: 0
Views: 848
Reputation: 56
I don't have too much time for now, but the following program should help you solve the first point. It is strictly based on your example. From your example input, I replaced alphabetic indexes i,j,k and l with 1,2,3,4, so the test file contains :
*my_first_key: {1}text
my_first_key: {2}different_text
my_first_key: {3}some_text
my_second_key: value1
my_first_key: {4}some_text
my_second_key: value2*
and the program is :
BEGIN {
# split on braces to have the index from my_first_key: in field #2 without effort
FS="[{}]"
}
/^my_first_key:/ { ix=$2}
/^my_second_key:/ {
sub(/^my_second_key: */, "") # leaves only value2 in $0
if (first_done==0) {
buffer["index1"]=ix
buffer["value1"]= $0
first_done=1
}
else { # first_done == 1
printf ("{%s:%s, %s:%s}\n", buffer["index1"], buffer["value1"], ix, $0)
first_done=0
}
}
and the output is:
awk -f example.awk test.data
{3:value1, 4:value2}
Upvotes: 3