一种可能性:
script.awk 的内容(带有 cmets):
## When 'FNR == NR', the first input file is in process.
## If line begins with '[', get the section string and reset the position
## of its objects.
FNR == NR && $0 ~ /^\[/ {
object = substr( $0, 2, length($0) - 2 )
pos = 0
next
}
## This section process the objects of each section. It saves them in
## an array. Variable 'pos' increments with each object processed.
FNR == NR {
arr_obj[object, $0] = ++pos
next
}
## This section process second file. It splits line in '.' to find second
## part in the array and prints all.
FNR < NR {
ret = split( $0, obj, /\./ )
if ( ret != 2 ) {
next
}
printf "%s.%d\n", obj[1], arr_obj[ obj[1] SUBSEP obj[2] ]
}
运行脚本(输入文件的顺序很重要,object.txt 包含对象部分和 input.txt 调用部分):
awk -f script.awk object.txt input.txt
结果:
SomeSection.2
OtherSection.1
OtherSection.2
编辑到 cmets 中的一个问题:
我不是专家,但我会尝试解释我是如何理解它的:
SUBSEP 是当您想使用不同的值作为键时分隔数组中索引的字符。默认为\034,但您可以修改为RS 或FS。
在指令arr_obj[object, $0] = ++pos 中,逗号将所有值与SUBSEP 的值连接起来,因此在这种情况下会导致:
arr_obj[SomeSection\034Blah] = 1
在脚本的末尾,我使用变量arr_obj[ obj[1] SUBSEP obj[2] 显式访问索引,但与上一节中的arr_obj[object, $0] 具有相同的含义。
您还可以访问该索引的每个部分,使用 SUBSEP 变量将其拆分,如下所示:
for (key in arr_obj) { ## Assign 'string\034string' to 'key' variable
split( key, key_parts, SUBSEP ) ## Split 'key' with the content of SUBSEP variable.
...
}
结果为:
key_parts[1] -> SomeSection
key_parts[2] -> Blah