ファイル分割ファイルの2つのフィールドの比較

ファイル分割ファイルの2つのフィールドの比較

最初の2つの列を比較する必要がある2つのファイルがあります。

入力ファイルの例1:

|CID|SID|order|orderdate|confirmdate
|151244127820|7177638911583| 2|2015-02-04 07:14:44|2015-02-04 07:15:32
|151244127820|7177638922976| 4|2015-02-04 07:16:19|2015-02-04 07:19:47
|151244127824|7177638920385| 2|2015-02-04 07:14:22|2015-02-04 07:18:48
|151244127824|7177638924073| 3|2015-02-04 07:18:40|2015-02-04 07:20:11
|151244127825|7177638921040| 1|2015-02-04 07:12:58|2015-02-04 07:19:02
|151244127827|7177638917056| 2|2015-02-04 07:14:17|2015-02-04 07:17:31
|151244127827|7177638968972| 3|2015-02-04 07:17:36|2015-02-04 07:36:22

入力ファイル2:

|cID|SID|order|orderdate|confirmdate
|151244127820|7177638911583|   2|2015-02-04 07:14:44|2015-02-04 07:15:32
|151244127820|7177638922976|   4|2015-02-04 07:16:19|2015-02-04 07:19:47
|151244127834|7177638920385|   2|2015-02-04 07:14:22|2015-02-04 07:18:48
|151244127834|7177638924073|   3|2015-02-04 07:18:40|2015-02-04 07:20:11
|151244126585|7177638921040|   1|2015-02-04 07:12:58|2015-02-04 07:19:02
|151244126585|7177638917056|   2|2015-02-04 07:14:17|2015-02-04 07:17:31
|151244127827|7177638968970|   3|2015-02-04 07:17:36|2015-02-04 07:36:22

入力 file2 の CID が file1 で見つからない場合は、行全体が新しいファイルに書き込まれます。入力 file2 の CID が file1 にあるが SID がない場合、行全体が新しいファイルに書き込まれます。

ベストアンサー1

awk -F'|' 'FNR==NR{a[$2]=$2;b[$3]=$3;next};{if($2 in a){print $0 > "new_file_1"};if(($2 in a )&& !($3 in b)){print $0 > "new_file_2"}} file1 file2

詳細...

{if($2 in a){print $0 > "new_file_1"} : if SID in file2  matches SID in file1 redirect to a file called new_file_1

..

if(($2 in a )&& !($3 in b)){print $0 > "new_file_2"} :if SID in file2 matches SID in file1 but CIDs does not match, redirect to a file called new_file_2

おすすめ記事