簡體   English   中英

連接文件並創建索引

[英]join files and create index

我想加入兩個文件並在新列中為它們建立索引,如下所示:

文件A

apple     1 2 3 4 5 6
banana    3 2 4 4 5 6
orange    2 3 4 5 6 7
pear      2 4 5 6 3 5

文件B

apple    1 3 4 5 6 7
grapes   4 5 6 4 3 6
melon    3 4 5 2 5 1
orange   2 4 5 6 7 8

我想比較基於前兩列的兩個文件,並輸出文件A的公共行,然后添加文件A和文件B的唯一行並為其編制索引,如下所示

輸出:

apple     1 2 3 4 5 6 both
orange    2 3 4 5 6 7 both
banana    3 2 4 4 5 6 fileA
pear      2 4 5 6 3 5  fileA
grapes   4 5 6 4 3 6   fileB
melon    3 4 5 2 5 1   fileB

comm比較兩個文件,並報告哪些行出現在一個,另一個或兩個行中:

comm -2 <(cut -f1 -d' ' fileA | sort)
        <(cut -f1 -d' ' fileB | sort) \
  | sed $'/\t/{s/$/ both/;s/\t//};/ /!s/$/ fileA/' \
  | join -o1.1,2.2,2.3,2.4,2.5,2.6,2.7,1.2 - fileA \
  | sort -k8
cut -f1 -d' ' fileA \
  | grep -vFf- fileB \
  | sed 's/$/ fileB/'

如您所見,管道非常復雜。 如果您打算更改代碼,請考慮使用功能更強大的語言,例如Perl。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM