Shell合并两个文件

时间:2017-11-01 15:08:40

标签: linux shell join awk

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

文件1:

5439725407 uQkiJRPOZLLJkc
5368657511 eWGDnOcNgxjBK
5322202068 dNsUkWOMk9lNJ

file2的:

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

OUTPUTFILE:

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

3 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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