简体   繁体   English

嵌套AWK数组的替代方案?

[英]Alternative to nested AWK arrays?

file1 is 文件1是

foo"blahblah#AAA17"blahblah
foo"blahblah#BBB64"blahblah
foo"blahblah#CCC53"blahblah
foo"blahblah#DDD59"blahblah
foo"blahblah#EEE22"blahblah
foo"blahblah#FFF49"blahblah

file2 is tab-separated and is file2是制表符分隔的,并且是

AAA17   something9197
BBB64   something333
CCC53   something268
DDD59   something5050
EEE22   something4643
FFF49   something2

I want to add some text to each of the lines in file1. 我想向file1中的每一行添加一些文本。 The text will include field 2 from file2, as shown below. 文本将包含file2中的字段2,如下所示。 Using gawk I can get the desired result with nested arrays: 使用gawk,我可以使用嵌套数组获得所需的结果:

awk -F"\t" 'FNR==NR {arr1[$1]=$2; next} {split($0,arr2,"#|\"")} {print $0": moomoo "arr1[arr2[3]]}' file2 file1
foo"blahblah#AAA17"blahblah: moomoo something9197
foo"blahblah#BBB64"blahblah: moomoo something333
foo"blahblah#CCC53"blahblah: moomoo something268
foo"blahblah#DDD59"blahblah: moomoo something5050
foo"blahblah#EEE22"blahblah: moomoo something4643
foo"blahblah#FFF49"blahblah: moomoo something2

Is there a simple way to do this that does not involve nested arrays? 有没有一种不涉及嵌套数组的简单方法?

Note that the two files might have their lines shuffled - not in order as shown here. 请注意,这两个文件的行可能会乱码-此处显示的顺序不正确。

All you need is: 所有你需要的是:

$ awk 'NR==FNR{arr[$1]=$2;next} {print $0 ": moomoo", arr[$3]}' file2 FS='[#"]' file1
foo"blahblah#AAA17"blahblah: moomoo something9197
foo"blahblah#BBB64"blahblah: moomoo something333
foo"blahblah#CCC53"blahblah: moomoo something268
foo"blahblah#DDD59"blahblah: moomoo something5050
foo"blahblah#EEE22"blahblah: moomoo something4643
foo"blahblah#FFF49"blahblah: moomoo something2

Your solution looks quite good already, assuming the files aren't too large. 假设文件不是太大,您的解决方案看起来已经不错。 "Nested arrays" is not time-penalizing the way "nested loops" are. “嵌套数组”不会像“嵌套循环”那样在时间上浪费时间。 But there is a another way to do it using the builtin join , although I don't know if I'd call it "simpler": 但是还有另一种使用内置join ,尽管我不知道是否称其为“简单”:

cat file1|awk -F"#|\"" '{print $3,$0": moomoo"}'|sort >file3
sort file2|join file3 -|cut -d " " -f 2-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM