处理两个文件时在AWK中使用数组 [英] Using an array in AWK when working with two files
问题描述
我有两个文件,我使用下面的代码合并了它们的密钥
I have two files I merged them based key using below code
file1
-------------------------------
1 a t p bbb
2 b c f aaa
3 d y u bbb
2 b c f aaa
2 u g t ccc
2 b j h ccc
file2
--------------------------------
1 11 bbb
2 22 ccc
3 33 aaa
4 44 aaa
我使用下面的代码合并了这两个基于文件的密钥
I merged these two file based key using below code
awk 'NR==FNR{a[$3]=$0;next;}{for(x in a){if(x==$5) print $1,$2,$3,$4,a[x]};
我的问题是我如何将$ 2的file2保存在变量或数组中,并在a [x]之后再次打印.
我想要的结果是:
My question is how I can save $2 of file2 in variable or array and print after a[x] again.
My desired result is :
1 a t p 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
3 d y u 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
2 u g t 2 22 ccc 22
2 b j h 2 22 ccc 22
您看到的前7列是我的合并代码的结果.我需要在结果中添加最后一列(a [x]的字段2).
As you see the first 7 columns is the result of my merge code. I need add the last column (field 2 of a[x]) to my result.
重要:
我的下一个问题是,如果我有.awk文件,如何使用某些bash脚本代码,例如( | column -t
)或将结果发送到文件( awk ...> result.txt
)?我总是在命令提示符下使用这些代码.我可以在.awk文件的代码中使用它们吗?
My next question is if I have .awk file, how I can use some bash script code like (| column -t
) or send result to file (awk... > result.txt
)? I always use these codes in command prompt. Can I use them inside my code in .awk file?
推荐答案
只需将所有 file2
添加到数组中,然后使用 split
保留所需的位:
Simply add all of file2
to an array, and use split
to hold the bits you want:
awk 'FNR==NR { two[$0]++; next } { for (i in two) { split(i, one); if (one[3] == $NF) print $1,$2,$3,$4, i, one[2] } }' file2 file1
结果:
1 a t p 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
3 d y u 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
2 u g t 2 22 ccc 22
2 b j h 2 22 ccc 22
关于您的最后一个问题;您还可以在 awk
的内部添加管道"和写入".这是到 column -t
的管道的示例:
Regarding your last question; you can also add 'pipes' and 'writes' inside of your awk
. Here's an example of a pipe to column -t
:
script.awk 的内容:
Contents of script.awk
:
FNR==NR {
two[$0]++
next
}
{
for (i in two) {
split(i, one)
if (one[3] == $NF) {
print $1,$2,$3,$4, i, one[2] | "column -t"
}
}
}
运行方式: awk -f script.awk file2 file1
将以下内容添加到您的shell脚本中:
Add the following to your shell script:
results=$(awk '
FNR==NR {
two[$0]++
next
}
{
for (i in two) {
split(i, one)
if (one[3] == $NF) {
print $1,$2,$3,$4, i, one[2] | "column -t"
}
}
}
' $1 $2)
echo "$results"
运行方式:
./script.sh file2.txt file1.txt
结果:
1 a t p 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
3 d y u 1 11 bbb 11
2 b c f 3 33 aaa 33
2 b c f 4 44 aaa 44
2 u g t 2 22 ccc 22
2 b j h 2 22 ccc 22
这篇关于处理两个文件时在AWK中使用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!