-3

現在、bash で 2 つのフラット ファイルを比較しようとしています。最初のファイルには、| で区切られた 3 つの列があります。2 つ目は | で区切られた 2 つの列を持ちます。2番目のファイルから欠落している入力を取得して、最初のファイルに入れたいと思います。不足している 2 つの列をファイル 2 からファイル 1 に引き継ぐことだけに関心があります。

サンプルファイル

ファイル 1:

a|青|3

b|黄|1

c|緑|2

ファイル 2:

あ|青

b|黄色

c|グリーン

d|紫

出力ファイル:

a|青|3

b|黄|1

c|緑|2

d|紫

4

1 に答える 1

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
于 2013-07-22T15:18:45.007 に答える