如果第一个文件/bash脚本中存在,则通过键合并两个文件 [英] merge two files by key if exists in the first file / bash script

查看:18
本文介绍了如果第一个文件/bash脚本中存在,则通过键合并两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,其中的列按第一列的值排序,我想仅当第二个值存在于第一个列时才合并它们.

i have two files with columns sorted by the value of the first column, and i want to merge them only if the value of the second exists in the first one.

第一个文件是这样的

man01 xxx yyy zzz
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

第二个文件

man01 sss
man08 sss

所需的输出是

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

我尝试加入但要求第一个文件中存在第二个文件的值:/

I tried join but requires values of second file exist in the first one :/

推荐答案

Join 可以做到这一点,你考虑过 -a 选项吗?它将为 a.txt 和 b.txt 中每个不成对的文件行生成一行.

Join can do this, have you considered the -a option ? It will produce a line for each unpairable file line in a.txt and b.txt.

join -a1 a.txt b.txt

man01 xxx yyy zzz sss
man02 xxx yyy zzz
man03 xxx yyy zzz
man04 xxx yyy zzz

这篇关于如果第一个文件/bash脚本中存在,则通过键合并两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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