加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix:使用第二列合并2个文件

发布时间:2020-12-15 21:50:43 所属栏目:安全 来源:网络整理
导读:我想根据第二列的内容合并两个文件. 档案1: "4742" "209220_at" 2.60700394801826"104" "209396_s_at" 2.60651442103297"749" "202409_at" 2.59424724783704"4168" "209875_s_at" 2.58773204877464"3973" "1431_at" 2.52832098784342"1826" "207201_s_at" 2
我想根据第二列的内容合并两个文件.

档案1:

"4742"  "209220_at"     2.60700394801826
"104"   "209396_s_at"   2.60651442103297
"749"   "202409_at"     2.59424724783704
"4168"  "209875_s_at"   2.58773204877464
"3973"  "1431_at"       2.52832098784342
"1826"  "207201_s_at"   2.41685345240968

文件2:

"653"   "1431_at"       2.14595534191867
"1109"  "207201_s_at"   2.13777517447307
"353"   "212531_at"     2.12706340284672
"381"   "206535_at"     2.11456707231618
"1846"  "204534_at"     2.10919474441178

到最后:

"3973"  "1431_at"       2.52832098784342 "653"   "1431_at"       2.14595534191867
"1826"  "207201_s_at"   2.41685345240968 "1109"  "207201_s_at"   2.13777517447307

我尝试过comm,diff,一些不起眼的awk单行,没有任何成功.
任何帮助非常感谢.

解决方法

awk '
  # store the first file,indexed by col2
  NR==FNR {f1[$2] = $0; next}
  # output only if file1 contains file2's col2
  ($2 in f1) {print f1[$2],$0}
' file1 file2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读