열 순서를 재정렬하지 않고 필드를 기반으로 두 파일을 결합하는 방법은 무엇입니까?

열 순서를 재정렬하지 않고 필드를 기반으로 두 파일을 결합하는 방법은 무엇입니까?

두 개의 입력 파일의 열을 포함하는 파일을 만들고 싶습니다. 파일 1은 다음과 같습니다.

s,a
k,b
h,c

파일 2는 다음과 같습니다.

f,a
g,b 

출력은 다음과 같아야 합니다.

s,a,f
k,b,g
h,c,-

나는 이런 Join 명령을 사용합니다

join  -a1 -a2 -t , -1 2 -2 2 -o auto -e "-" file1 file2 > joinoutput

나는 다음과 같이 나갔다

a,s,f
b,k,g
c,h,-

문제를 해결하도록 도와주세요. -o '1.1'과 같이 열 순서를 지정할 수 없습니다. 첫 번째 파일의 열 수가 n이고 두 번째 파일의 경우 n+n -1을 확인해야 합니다. 미리 감사드립니다.

답변1

-o auto사용. . . 교체-o '1.1 1.2 2.1'

얻으려면 :

s,a,f
k,b,g
h,c,-

답변2

다음 perl과 같이 :

#!/usr/bin/env perl
use strict;
use warnings;

#Data Dumper is for diagnostic printing
use Data::Dumper;


#open second file for reading
open( my $file2, '<', 'sampleb.txt' ) or die $!;

#slurp this file into a hash (key value map) - reverse them, so we use
#the second value as a lookup key. 
my %key_values = reverse map {/(\w+),(\w+)/} <$file2>;
close($file2);

#print what we got in that map for diag reasons. 
print Dumper \%key_values;

 #open the first file
open( my $file1, '<', 'samplea.txt' ) or die $!;
#iterate by line
while (<$file1>) {
    chomp; #strip trailing line feed. 
    #split this line on comma
    my ( $key, $value ) = split /,/;
    #loopup "$value" in that hash we created - use that if it's defined, or '-' otherwise. 
    print join( ",", $key, $value, $key_values{$value} // '-' ), "\n";
}
close ( $file1 ); 

산출:

s,a,f
k,b,g
h,c,-

관련 정보