比较awk中两个文件的字段 [英] Comparing fields of two files in awk

查看:179
本文介绍了比较awk中两个文件的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较两个文件的两个字段,如下所示:

I want to compare two fields of two files, such as follows:

将文件1的第二个字段与文件2的第一个字段进行比较,打印匹配项(即使重复匹配),并打印文件一和第二的所有列.

Compare the 2nd filed of file one with the 1st field of file two, print the match (even if the match is repeated) and all the columns of file one and two.

文件1:

 G4   b45  3  4
 G4   b45  1  3
 G3   b23  2  2
 G3   b22  2  6
 G3   b22  2  4

文件2:

 b45  a  b  c
 b64  d  e  f  
 b23  g  h  i
 b22  j  k  l
 b20  m  n  o     

输出:

 G4   b45  a  b  c  3  4
 G4   b45  a  b  c  1  3
 G3   b23  g  h  i  2  2
 G3   b22  j  k  l  2  6
 G3   b22  j  k  l  2  4

我已经使用关联数组通过以下awk命令尝试了此操作:

I have tried this with the following awk command using associative arrays:

awk 'FNR==NR {array1[$2] = $1 ; arrayrest[$2] = substr($0, index($0, $2)); next}($1 in array1) {print array1[$1] "\t" $0 "\t" arrayrest[$1]}' file1 file2

但是有两个问题:

  1. 如果我希望打印重复的行,则不会打印这些行.
  2. 它在输出中重复文件2的第一个字段.

如何使该awk命令正常工作?预先感谢.

How could I make this awk command work nicely? Thanks in advance.

推荐答案

不是您想要的确切输出格式,而是正确的输出内容.

Not quite the exact output formatting you want but the right output contents.

awk 'FNR==NR{seen[$1]=$0; next} ($2 in seen) {$2=seen[$2]}7' file2 file1

添加| column -t以获得更一致的列间距.

Add | column -t to get more consistent column spacing.

这篇关于比较awk中两个文件的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆