简体   繁体   中英

join files and create index

I want to join two files and index them in a new column as show below :

file 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

file 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

I want to compare the two files based on first two columns, and output the common rows from file A and then add unique rows from both file A and file B and index them as shown below

output:

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 compares two files and reports which lines occur in one, the other or both:

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/'

As you can see, the pipeline is pretty complicated. If you plan to make changes to the code, consider moving to a more powerful language like Perl.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM