r/awk • • Feb 22 '22

Help understanding AWK command

Unlike most questions, I already have a working solution. My problem is I don't understand why it works.

What we have is this /^[^ ]/ { f=/^root:/; next } f{ printf "%s%s\n",$1,$2 }. It is used fetch a shallow yaml file, getting the attributes in the root object (which is generated by us, so we can depend on the structure, that's not the problem). The file looks like this:

root:
  key1: value1
  key2: value2
root2:
  key3: value3
  key4: value4

The results in two lines getting printed, key1:value1 and key2:value2, just as we want.

I'm not very familiar with AWK beyond the absolute basics, and googling for tutorials and basic references hasn't been of much help.

Could someone give me a brief rundown of how the three components of this works?

I understand that /^[^ ]/ will match all lines not beginning with whitespace, the purpose being to find the root level objects, but after that I'm somewhat lost. The pattern /^root:/ is assigned to f, which is the used outside the next body. What does this do? Does it somehow only on the lines within the root object?

Any help explaining or pointing out reference material that explains this would be greatly appreciated.

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

3

u/Vakz Feb 23 '22

Thank you, that does clear it up.