我有两个文件
档案A:
John
Janice
Carry
Ethan
档案B:
2 Ronny
1 Michelle
2 John
2 Janice
10 Carry
1 Ethan
我想从文件B到文件A获取第一列(数字),以便我可以拥有此类文件A
2 John
2 Janice
10 Carry
1 Ethan
我怎样才能使用bash?
答案 0 :(得分:1)
以下awk
单行代码可以解决问题:
$ awk 'FNR==NR{a[$2]=$1;next}($0 in a){print a[$0],$0}' file2 file1
2 John
2 Janice
10 Carry
1 Ethan
答案 1 :(得分:0)
您可以尝试Perl
:
open( A, "<", "A.txt" ) or die "Cannot open A.txt\n"; # open A.txt for reading
open( B, "<", "B.txt" ) or die "Cannot open B.txt\n"; # open B.txt for reading
chomp( my @A = <A> ); # get rid of newlines
while(my $bline = <B>){ # iterate B.txt
chomp( $bline ); # get rid of newline
@ha = split /\s+/, $bline; # split to get 2nd field (name)
if ( grep{ /$ha[1]/ } @A ) { # if 2nd field found in A array
print $bline . "\n";
}
}
close( A ); # close A file descriptor
close( B ); # close B file descriptor