比较linux(bash)中的两个平面文件,将丢失的内容放在第一个文件中?

时间:2013-07-22 14:48:46

标签: linux bash scripting

我目前正在尝试比较bash中的两个平面文件。第一个文件将有三列以|分隔第二个将有两列用|分隔。我想从第二个文件中获取缺少的输入并将其放入第一个文件中。我只关心将文件2中的两个缺失列接管到文件1。

示例文件

文件一:

  

一个|蓝色| 3

     

C |黄色| 1

     

C ^ |绿色| 2

文件二:

  

一个|蓝色

     

C |黄

     

C ^ |绿色

     

d |紫

输出文件:

  

一个|蓝色| 3

     

C |黄色| 1

     

C ^ |绿色| 2

     

d |紫

1 个答案:

答案 0 :(得分:1)

这应该有效:

# Set the input field separator to "|"
awk -F'|' '

# Load the second file into an array called "a". NR==FNR allows us to perform this action
# until first file is complete 
NR==FNR { a[$0]; next }

# We check the existence of first and second column of first file in array. If it is present
# we delete that array element. 1 at the end allows us to print the line from first file as is. 
($1 FS $2 in a) { delete a[$1 FS $2] }1

# This action takes place at the very end. Whatever is left in our array we iterate through
# and print it. This can cause the output to appear in any order hence sort is needed. 
END { for (l in a) print l }' f2 f1

<强>输出

$ head f*
==> f1 <==
a|blue|3
c|green|2
b|yellow|1

==> f2 <==
a|blue
c|green
b|yellow
d|purple

$ awk -F'|' '
NR==FNR { a[$0]; next }
($1 FS $2 in a) { delete a[$1 FS $2] }1
END { for (l in a) print l }' f2 f1
a|blue|3
c|green|2
b|yellow|1
d|purple