搭配两列并把它们放在一个文件 [英] Match two columns and put them in one file

查看:69
本文介绍了搭配两列并把它们放在一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件。在每个文件中我有两列。我需要的file1两列的第一值与来自file2的两列中的每个值匹配。如果它们相等我需要把两列(一列从文件1和file2)在一个文件中,但它们应该彼此相邻。如果这两个值不匹配,什么也不做。

文件1

  0 344
465 1
729 2
777 3
676 4
862 5

文件2

  0 766
937 1
980 2
837 3
936 5

输出示例:

  344 766
465 937
729 980
777 837
862 936


解决方案

 的awk'NR == FNR {值[$ 2] = $ 1;下一个; }
             {如果($ 2的值)打印值[$ 2,$ 1}'文件1文件2

虽然记录数等于该文件记录号(在读取第一个文件),在第1列藏匿值值[$ 2] ,跳到下一个记录。在处理第二个文件,如果有一个在值[$ 2]的值,打印出来的值值[$ 2] $ 1

I have two files. In each file I have two columns. I need to match the first value of column two of file1 with each value from column two of file2. If they are equal I need to put the two columns (column one from file1 and file2) in one file, but they should be adjacent to each other. If the two values do not match, do nothing.

file1

344  0
465  1
729  2
777  3
676  4
862  5

file2

766  0
937  1
980  2
837  3
936  5

Example output:

344    766
465    937
729    980
777    837
862    936

解决方案

awk 'NR==FNR { values[$2] = $1; next; }
             { if ($2 in values) print values[$2], $1 }' file1 file2

While the record number equals the file record number (while reading first file), stash values in column 1 in values[$2], skipping to the next record. While processing the second file, if there's a value in values[$2], print out the value in values[$2] and $1.

这篇关于搭配两列并把它们放在一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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