比较两个文件,​​如果匹配下移最后一个字段(AWK) [英] Compare two files, if match shifted down last fields (awk)

查看:86
本文介绍了比较两个文件,​​如果匹配下移最后一个字段(AWK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个输入文件(制表符分隔),我需要在它们之间找到匹配的$ 1安培;&安培; $ 2如果只匹配第三和第四场将下移:

I have two input files (tab delimited) and i need find match between them for the $1 && $2 if the match only 3rd and 4th field will be shifted down :

输入:
文件1:

INPUT: File1:

 p1   555  
 p1   557  
 p3   558

文件2:

p1  323 lololo  aaaa    
p1  555 papapp  kkka    
p1  556 hooho   sssa    
p1  557 jjjlo   kkka    
p3  424 zzzzz   llla    
p3  558 jjjjj   ssss

OUTPUT:

OUTPUT:

p1 323  lololo aaaa
p1 555
p1 556  papaapp kkka
p1 557   
p3 424  hooho   sssa
p3 558      
        jjjlo   kkka  

等。

感谢您

推荐答案

沿着这些线路的东西应该工作:

Something along these lines should work:

awk 'NR == FNR { to_shift[$1,$2] = 1; next } { queue[++w] = $3 OFS $4 } to_shift[$1, $2] { print $1, $2; next } { print $1, $2, queue[++r] } END { while(r != w) { print OFS OFS queue[++r] } }' file1 file2

这就是:

NR == FNR {                      # while processing the first file (file1)
  to_shift[$1,$2] = 1            # remember which lines to shift
  next                           # and do nothing else
}
{                                # afterwards (processing file2):
  queue[++w] = $3 OFS $4         # queue the next payload fields
}
to_shift[$1, $2] {               # If this is a shift line
  print $1, $2                   # print only the first two fields
  next                           # and do nothing else
}
{                                # otherwise, print the first two fields and
  print $1, $2, queue[++r]       # the next queued payload
}
END {                            # In the end:
  while(r != w) {                # print out what remains in the queue, i.e.
    print OFS OFS queue[++r]     # all that was shifted out at the bottom
  }
}

我怀疑格式化您可能需要使用 \\ t 作为输出域,在这种情况下,你可以简单地传递 -v OFS = \\ t AWK

I suspect that for formatting you may want to use \t as output field separator, in which case you could simply pass -v OFS='\t' to awk:

awk -v OFS='\t' 'NR == FNR { to_shift[$1,$2] = 1; next } { queue[++w] = $3 OFS $4 } to_shift[$1, $2] { print $1, $2; next } { print $1, $2, queue[++r] } END { while(r != w) { print OFS OFS queue[++r] } }' file1 file2

如果输入的是制表符分隔和字段可以包含空格,也通过 -F'\\ t'来使输入字段分隔标签为好。

If the input is tab-separated and fields can contain spaces, also pass -F '\t' to make the input field separator a tab as well.

这篇关于比较两个文件,​​如果匹配下移最后一个字段(AWK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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