I have an input file like this:
SomeSection.Foo
OtherSection.Foo
OtherSection.Goo
…and there is another file describing which object(s) belong to each section:
[SomeSection]
Blah
Foo
[OtherSection]
Foo
Goo
The desired output would be:
SomeSection.2 // that's because Foo appears 2nd in SomeSection
OtherSection.1 // that's because Foo appears 1st in OtherSection
OtherSection.2 // that's because Goo appears 2nd in OtherSection
(The numbers and names of sections and objects are variable)
How would you do such a thing in awk?
Thanks in advance,
Adrian.
One possibility:
Content of script.awk (with comments):
Run the script (important the order of input files, object.txt has sections with objects and input.txt the calls):
Result:
EDIT to a question in comments:
I’m not an expert but I will try to explain how I understand it:
SUBSEPis a character to separate indexes in an array when you want to use different values as key. By default is\034, although you can modify it likeRSorFS.In instruction
arr_obj[object, $0] = ++posthe comma joins all values with the value ofSUBSEP, so in this case would result in:At the end of the script I access to the index using explicity that variable
arr_obj[ obj[1] SUBSEP obj[2], but with same meaning thatarr_obj[object, $0]in previous section.You can also access to each part of this index splitting it with SUBSEP variable, like this:
with a result of: