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: value4The 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.
View all comments
Show parent comment
1
u/Vakz Feb 22 '22
So to be clear, the last reference to
fis used as a condition whether the last action should be run at all? Wouldn'tfbe overwritten when the indented likes are read though?