gpt4 book ai didi

awk - 使用 awk 进行 vlookup : how to match groups/patterns with ids

转载 作者:行者123 更新时间:2023-12-02 02:42:16 25 4
gpt4 key购买 nike

我正在使用 awk,并且想要对与主列表 id 匹配的组模式执行 vlookup 之类的操作

master.txt

1.1.1: name00
1.1.2: name01
1.1.3: name02
1.1.4: name03
1.1.5: name04
1.2.2: name05
1.2.3: name06
1.2.4: name07
1.2.5: name08
1.2.6: name09
1.3.13: name10
1.3.14: name11
1.3.15: name12
1.3.16: name13
1.3.17: name14

group.txt

1.1: groupvalue0
1.2: groupvalue1
1.3: groupvalue2
1.1: groupvalue10
1.3: groupvalue12

每个组可以有多个值

1.1对应的主数据:这里 (^1.1.) 匹配下面的内容(仅从头开始匹配)

1.1.1: name00
1.1.2: name01
1.1.3: name02
1.1.4: name03
1.1.5: name04

1.2 类似这里 (^1.2.) 匹配下面的内容(仅从头开始匹配)

1.2.2: name05
1.2.3: name06
1.2.4: name07
1.2.5: name08
1.2.6: name09

根据匹配,我希望结果为

name00: groupvalue0  # 1.1.1 just shown for reference
name01: groupvalue0 # 1.1.2 just shown for reference
name02: groupvalue0 # 1.1.3 just shown for reference
name03: groupvalue0 # 1.1.4 just shown for reference
name04: groupvalue0 # 1.1.5 just shown for reference
name05: groupvalue1 # 1.2.2 just shown for reference
name06: groupvalue1 # 1.2.3 just shown for reference
name07: groupvalue1 # 1.2.4 just shown for reference
name08: groupvalue1 # 1.2.5 just shown for reference
name09: groupvalue1 # 1.2.6 just shown for reference
name10: groupvalue2 # 1.3.13 just shown for reference
name11: groupvalue2 # 1.3.14 just shown for reference
name12: groupvalue2 # 1.3.15 just shown for reference
name13: groupvalue2 # 1.3.16 just shown for reference
name14: groupvalue2 # 1.3.17 just shown for reference
name00: groupvalue10 # 1.1.1 just shown for reference
name01: groupvalue10 # 1.1.2 just shown for reference
name02: groupvalue10 # 1.1.3 just shown for reference
name03: groupvalue10 # 1.1.4 just shown for reference
name04: groupvalue10 # 1.1.5 just shown for reference
name10: groupvalue12 # 1.3.13 just shown for reference
name11: groupvalue12 # 1.3.14 just shown for reference
name12: groupvalue12 # 1.3.15 just shown for reference
name13: groupvalue12 # 1.3.16 just shown for reference
name14: groupvalue12 # 1.3.17 just shown for reference

我正在尝试使用下面的 awk 代码。但如何在数组中使用模式。

BEGIN{
FS=": "
#print(var)
}
{
if(NR==FNR) # process first file only
{
a[$1]=$2; # hash to a array {id is key, name} value
next; # process next record without executing following code
} else
{ # process second file
pattern= "^"$1"\..*" # eg: ^1\.1\..*
# can i use pattern in array
print a[pattern]":",$2 # output name (the value of) from array a and property
}

} master.txt group.txt

最佳答案

即使您的输入包含其他 : 或空格,而不仅仅是示例中显示的内容,以下内容也将起作用:

$ cat tst.awk
BEGIN { OFS=": " }
{
group = substr($0,match($0,/[0-9]+\.[0-9]+/),RLENGTH)
name = substr($0,match($0,/[^:]+: */)+RLENGTH)
}
NR==FNR {
map[group,++cnt[group]] = name
next
}
{
for (i=1; i<=cnt[group]; i++) {
print map[group,i], name
}
}

.

$ awk -f tst.awk master.txt group.txt
name00: groupvalue0
name01: groupvalue0
name02: groupvalue0
name03: groupvalue0
name04: groupvalue0
name05: groupvalue1
name06: groupvalue1
name07: groupvalue1
name08: groupvalue1
name09: groupvalue1
name10: groupvalue2
name11: groupvalue2
name12: groupvalue2
name13: groupvalue2
name14: groupvalue2
name00: groupvalue10
name01: groupvalue10
name02: groupvalue10
name03: groupvalue10
name04: groupvalue10
name10: groupvalue12
name11: groupvalue12
name12: groupvalue12
name13: groupvalue12
name14: groupvalue12

关于awk - 使用 awk 进行 vlookup : how to match groups/patterns with ids,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63374583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com