【问题标题】:Merge two file based on common value in column根据列中的共同值合并两个文件
【发布时间】:2023-04-03 14:28:02
【问题描述】:

我有两个文件,file1file2

文件 1:

00451367210;518       ;
00140913111;21        ;
00551360550;418       ;
00550362618;16        ;
00850362809;13        ;

文件 2

00451367210;041;0
00140913111;021;0
00010010136;021;0
00210010157;041;1
00550362618;121;0
00850362809;021;0
00010010337;021;0
00551360551;021;0
00551360550;121;0

我想根据文件 1 和文件 2 中第 1 列的共同值合并两个文件的列

结果应该是这样的:

00451367210;041;0;518       ;
00140913111;021;0;21        ;
00551360550;121;0;418       ;
00550362618;121;0;16        ;
00850362809;021;0;13        ;

我已经试过了:

join -t";"  -o '0,1.2,1.3,2.2,2.3' File1 File2

但我有这个:

00451367210;041;0;518       ;
00140913111;021;0;21        ;
join: file 2 is not in sorted order
join: file 1 is not in sorted order
00850362809;021;0;13        ;

知道如何使用 awk 或 join 获得想要的结果吗?

【问题讨论】:

    标签: linux shell join awk


    【解决方案1】:

    使用 awk 完成工作:

    $ awk 'BEGIN{FS=OFS=";"}NR==FNR{a[$1]=$0;next}($1 in a)&&$1=a[$1]' file2 file1
    00451367210;041;0;518       ;
    00140913111;021;0;21        ;
    00551360550;121;0;418       ;
    00550362618;121;0;16        ;
    00850362809;021;0;13        ;
    

    解释:

      BEGIN { FS=OFS=";" }      # set delimiters 
    NR==FNR { a[$1]=$0; next }  # hash file 2 on first field to a 
    ($1 in a) && $1=a[$1]       # if file1 record is found in a output it
    

    如果您想探索您的join 路径,请尝试使用进程替换对数据进行排序:

    $ join -t";"  -o '0,1.2,1.3,2.2,2.3' <(sort file1) <(sort file2)
    00140913111;21        ;;021;0
    00451367210;518       ;;041;0
    00550362618;16        ;;121;0
    00551360550;418       ;;121;0
    00850362809;13        ;;021;0
    

    【讨论】:

      【解决方案2】:

      没有awk,如果你想保留顺序,join 可能会改变,这里有一个方法

      f() { nl -s';' $1 | sort -t';' -k2;}; 
      join -t';' -j2 <(f file1) <(f file2) -o1.2,2.3,1.3,1.1 | 
      sort -t';' -k4n | 
      sed -r 's/[ 0-9]+$//'
      
      
      00451367210;041;518       ;
      00140913111;021;21        ;
      00551360550;121;418       ;
      00550362618;121;16        ;
      00850362809;021;13        ;
      

      【讨论】:

        【解决方案3】:

        试试这个 -

        $ awk -F';' 'NR==FNR{a[$1]=$2;next} $1 in a {print $0 FS a[$1] OFS FS}' f1 f2
        00451367210;041;0;518        ;
        00140913111;021;0;21         ;
        00550362618;121;0;16         ;
        00850362809;021;0;13         ;
        00551360550;121;0;418        ;
        

        【讨论】:

          【解决方案4】:

          加入命令方式:

          join -t';' -j1 -o'1.1,2.2,2.3,1.2,1.3'  <(sort /tmp/file1) <(sort /tmp/file2)
          

          输出(排序):

          00140913111;021;0;21        ;
          00451367210;041;0;518       ;
          00550362618;121;0;16        ;
          00551360550;121;0;418       ;
          00850362809;021;0;13        ;
          

          【讨论】:

            猜你喜欢
            • 2019-03-01
            • 2014-03-31
            • 1970-01-01
            • 2016-01-12
            • 2017-09-03
            • 2016-01-20
            • 2021-12-17
            相关资源
            最近更新 更多