【问题标题】:Shell merge two file [closed]Shell合并两个文件[关闭]
【发布时间】:2017-11-01 15:08:39
【问题描述】:

我有两个文件,我想通过匹配 file1 和 file2 中的第二个和第一个字段来连接 file1 和 file2,并将第一个数据从 file1 写入 file2

文件1:

5439725407 uQkiJRPOZLLJkc
5368657511 eWGDnOcNgxjBK
5322202068 dNsUkWOMk9lNJ

文件2:

uQkiJRPOZLLJkc,00087b8dbe6fdc3a5725a0a77fa4e37f3db10440d8b0da2d3935cb0f8f4f9089,1
eWGDnOcNgxjBK,0008958b743f8b786fa7f080348f3180f8e410890a07995878c1fcbda66706b4,1
dNsUkWOMk9lNJ,0008bc14ecce6150bef44f657e12314b8b2c37a5730bf88fc81b66d5f77ed8be,1

输出文件:

uQkiJRPOZLLJkc,00087b8dbe6fdc3a5725a0a77fa4e37f3db10440d8b0da2d3935cb0f8f4f9089,1,5439725407
eWGDnOcNgxjBK,0008958b743f8b786fa7f080348f3180f8e410890a07995878c1fcbda66706b4,1,5368657511
dNsUkWOMk9lNJ,0008bc14ecce6150bef44f657e12314b8b2c37a5730bf88fc81b66d5f77ed8be,1,5322202068

【问题讨论】:

  • 在此处提问之前您是否查看了man join
  • OP,请始终按照论坛规则在帖子中使用代码标签作为示例输入和示例输出。
  • 是的,我做到了,但我做不到

标签: linux shell join awk


【解决方案1】:

关注awk 解决方案也可能对您有所帮助。

awk 'FNR==NR{a[$1]=$0;next} ($2 in a){print a[$2] "," $1}' FS="," filE2  FS=" " filE1

输出如下。

uQkiJRPOZLLJkc,00087b8dbe6fdc3a5725a0a77fa4e37f3db10440d8b0da2d3935cb0f8f4f9089,1,5439725407
eWGDnOcNgxjBK,0008958b743f8b786fa7f080348f3180f8e410890a07995878c1fcbda66706b4,1,5368657511
dNsUkWOMk9lNJ,0008bc14ecce6150bef44f657e12314b8b2c37a5730bf88fc81b66d5f77ed8be,1,5322202068

编辑:现在也在这里添加非单行解决方案形式的解释。

awk '
FNR==NR{                      ##Checking condition here FNR==NR, which will be TRUE when first Input_file will be read.
  a[$1]=$0;                   ##Creating an array named a whose index is first field of current line and value is current line.
  next                        ##next statement will skip all further statements.
}                             ##Following block will be executed when 2nd Input_file is being read.
($2 in a){                    ##checking if 2nd field of current line is present in array a, if yes then do following.
  print a[$2] "," $1          ##Printing the value of array a whose index is $2 of current line, printing comma and then printing first field of current line.
}
' FS="," filE2  FS=" " filE1  ##Setting field separator as comma for Input_file2 and setting field separator as space for Input_file1 here.

【讨论】:

    【解决方案2】:

    awk解决方案:

    awk 'NR==FNR{a[$2]=$1;next}{ print $1,$2,$3,($1 in a? a[$1]:"") }' file1 FS=',' OFS=',' file2
    

    输出:

    uQkiJRPOZLLJkc,00087b8dbe6fdc3a5725a0a77fa4e37f3db10440d8b0da2d3935cb0f8f4f9089,1,5439725407
    eWGDnOcNgxjBK,0008958b743f8b786fa7f080348f3180f8e410890a07995878c1fcbda66706b4,1,5368657511
    dNsUkWOMk9lNJ,0008bc14ecce6150bef44f657e12314b8b2c37a5730bf88fc81b66d5f77ed8be,1,5322202068
    

    【讨论】:

      【解决方案3】:

      我认为这就是你想要的(我认为你的 fileX 格式有点偏离)

          cat file2 | tr \, ' ' | join -1 1 -2 2 - file1 | tr ' ' \,
      

      【讨论】:

      • 你可以使用 tr ' ' ','
      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2018-02-25
      • 1970-01-01
      • 2014-07-18
      • 2014-07-21
      相关资源
      最近更新 更多