2012-07-12 71 views
1

一號文件格局的Unix匹配

D07  [email protected] 
B82  GG^333 
D84  [email protected] 
F59  LL$$EE 

第二個文件

D84  /usr/ss 
F59  /usr/jh 
B82  /usr/kk 
D07  /usr/ks 

輸出應該是

D84  [email protected]  /usr/ss 
F59  LL$$EE /usr/jh 
B82  GG^333  /usr/kk 
D07  [email protected]  /usr/ks 

基本上,我想第一個文件的數據排序的WRT來第二檔

我試過:

BEGIN { 
    FS = OFS = "\t"; 
} 
NR==FNR { 
    Values[$1+0] = $2; 
    next; 
} 
{ 
    $1 = $1 OFS ($1+0 in Values ? Values[$1+0] : ""); 
    print $0; 
} 
' 1stfile 2ndfile 

任何人都可以提供任何簡單的解決方案來實現這一目標嗎?

回答

1

你不需要perl的,如果你願意將文件的bash join

$ ll 
file1 
file2 
$ cat file1 
D07  [email protected] 
D82  GG^333 
D84  [email protected] 
D59  LL$$EE 
$ cat file2 
D84  /usr/ss 
D59  /usr/jh 
D82  /usr/kk 
D07  /usr/ks 
$ sort -n file1 > file1a 
$ sort -n file2 > file2a 
$ cat file1a 
D07  [email protected] 
D59  LL$$EE 
D82  GG^333 
D84  [email protected] 
$ cat file2a 
D07  /usr/ks 
D59  /usr/jh 
D82  /usr/kk 
D84  /usr/ss 
$ join file1a file2a > file3 
$ cat file3 
D07 [email protected] /usr/ks 
D59 LL$$EE /usr/jh 
D82 GG^333 /usr/kk 
D84 [email protected] /usr/ss 
+0

剛剛更新的問題......如果數據沒有排序...你能幫我解決這個問題嗎.. – Debaditya 2012-07-12 06:44:34

+0

當你第一次排序時,這是一個問題嗎?或者這會搞砸你想做的事情? – Bernhard 2012-07-12 06:48:53

+0

我想我得到的解決方案.... ii我想與sort -nd – Debaditya 2012-07-12 06:52:40

0

用Perl進行排序,然後使用:

perl -nle '/(.*?)\s+(.*)/&&($h{$1}[$t]=$2);eof&&$t++;END{while(($k,$v)=each%h){print"$k @{$v}"}}' 1stfile 2ndfile