2016-11-16 56 views
0

我有兩個文件看起來像:比較兩個文件,UNIX和打印數據

文件1:

JOB1/JOB1_Backup #45,45,SUCCESS,20161115T23:30:00 
    Testing/Linux_Git #113,113,SUCCESS,20161115T11:13:08 
    Sales and Distri/Master Booking/MCAB CI Pipe/Cloud_test #27,27,SUCCESS,20161115T17:40:38 
    Sales and Distri/Master Booking/MCAB CI Pipe/Common Components #36,36,SUCCESS,20161115T13:21:36 
    UF_Test/backup/JOB4/TEST_JOB_NEW #14,14,FAILURE,20161115T09:13:03 

文件2:

JOB1/JOB1_Backup 11223344 
Sales and Distri/Master Booking/MCAB CI Pipe/Cloud_test 22334455 
Sales and Distri/Master Booking/MCAB CI Pipe/Common Components 99887766 
JF_DEV/dev and test/job7 55667744 

輸出應該是:

11223344 JOB1/JOB1_Backup #45,45,SUCCESS,20161115T23:30:00 
      Testing/Linux_Git #113,113,SUCCESS,20161115T11:13:08 
22334455 Sales and Distri/Master Booking/MCAB CI Pipe/Cloud_test #27,27,SUCCESS,20161115T17:40:38 
99887766 Sales and Distri/Master Booking/MCAB CI Pipe/Common Components #36,36,SUCCESS,20161115T13:21:36 
      UF_Test/backup/JOB4/TEST_JOB_NEW #14,14,FAILURE,20161115T09:13:03 

迄今嘗試過:

awk 'FNR==NR {a[$1]; next} $1 in a' file1 file2 

任何幫助將appriciated

回答

1
awk ' 
    NR == FNR { 
    val = $NF   # save the last field 
    NF--    # discard the last field 
    value[$0] = val  # map the new line to the (previous) last field 
    next 
    } 
    { 
    val = "" 
    for (str in value) { 
     if (index($0, str) > 0) { 
     val = value[str] 
     break 
     } 
    } 
    printf "%-8s %s\n", val, $0 
    } 
' file2 file1 
1

試試這個 -

#awk 'NR==FNR{a[$1]=$NF;next;} {print ($0 ? a[$1] OFS $0 :$0)}' file2 file1 
11223344 JOB1/JOB1_Backup #45,45,SUCCESS,20161115T23:30:00 
    Testing/Linux_Git #113,113,SUCCESS,20161115T11:13:08 
99887766 Sales and Distri/Master Booking/MCAB CI Pipe/Cloud_test #27,27,SUCCESS,20161115T17:40:38 
99887766 Sales and Distri/Master Booking/MCAB CI Pipe/Common Components #36,36,SUCCESS,20161115T13:21:36 
    UF_Test/backup/JOB4/TEST_JOB_NEW #14,14,FAILURE,20161115T09:13:03 

解釋 -

{print ($0 ? a[$1] OFS $0 :$0)}使用三元操作員使用左外連接的結果。

+0

對於第一個銷售行,您的值有誤。 –