如果存在于第一个文件/ bash脚本,使用关键字合并两个文件 [英] merge two files by key if exists in the first file / bash script

查看:86
本文介绍了如果存在于第一个文件/ 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 :/

推荐答案

加入能做到这一点,你有没有考虑到 -a 选项?这将产生在A.TXT和b.txt每个unpairable文件行一行。

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天全站免登陆