如何合并来自不同文本文件的两列?

如何合并来自不同文本文件的两列?

text1

    ankr 
    ankr
    ankr
    brmu
    brmu

text2

    jplg1730.17i
    jplg1750.17i
    jplg1770.17i

我需要创建这个文本文件:

merged_file:
    ankr_jplg1730.17i
    ankr_jplg1750.17i
    ankr_jplg1770.17i
    brmu_jplg1730.17i
    brmu_jplg1750.17i

我如何将该文本文件与代码合并?

答案1

awk

awk 'NR==FNR {a[NR]=$0; next} {if (a[i]) {print $0"_"a[i]; ++i} \
                               else {print $0"_"a[1]; i=2}}' text2 text1
  • NR==FNR {a[NR]=$0; next}将记录text2作为值保存到关联数组中a,以相应的记录号作为键

  • {if (a[i]) {print $0"_"a[i]; ++i} else {print $0"_"a[1]; i=2}}以所需格式打印记录text1,并跟踪数组索引的迭代和变量i

例子:

% cat text1
ankr
ankr
ankr
brmu
brmu

% cat text2
jplg1730.17i
jplg1750.17i
jplg1770.17i

% awk 'NR==FNR {a[NR]=$0; next} {if (a[i]) {print $0"_"a[i]; ++i} else {print $0"_"a[1]; i=2}}' text2 text1
ankr_jplg1730.17i
ankr_jplg1750.17i
ankr_jplg1770.17i
brmu_jplg1730.17i
brmu_jplg1750.17i

答案2

使用python:

$ python3 -c 'import sys, itertools as i; [print(a.strip(), b.strip(), sep="_") for a,b in zip(open(sys.argv[1]), i.cycle(open(sys.argv[2])))]' text1 text2
ankr_jplg1730.17i
ankr_jplg1750.17i
ankr_jplg1770.17i
brmu_jplg1730.17i
brmu_jplg1750.17i

更清晰地:

#! /usr/bin/env python3
import sys
from itertools import cycle
with open(sys.argv[1]) as file1, open(sys.argv[2]) as file2:
    for l1, l2 in zip(file1, cycle(file2)):
        print(l1.strip(), l2.strip(), sep="_")

cycle(file2)循环的内容file2,以便在读取最后一行之后,从第一行重新开始。zip从每个文件中取出一行。

相关内容