Linux

使用 bash 在公共鍵列上左外連接 2 shell 輸出

  • September 8, 2020

有沒有一種方法可以根據公共鍵列(IP 地址)對來自 2 個不同命令的 bash 輸出進行“左外連接”?由於各種原因,我想避免將任一命令的輸出保存到文件中,並將文件名用作 awk/join 的輸入。

這些是我想合併在一起的 2 個輸出:

output1=$(oc get pod -lname=elasticsearch -o wide)
output2=$(curl -ks https://elasticsearch.apps.ocp.domain.com/_cat/allocation?v)

這是我的 elasticsearch pod 的磁碟使用率

$ echo "$output1"
shards disk.indices disk.used disk.avail disk.total disk.percent host          ip            node
  347        1.5tb     1.5tb      4.2tb      5.8tb           27 10.1.1.1      10.1.1.1      jxAuPMt
  335        1.7tb     1.7tb        4tb      5.8tb           30 10.2.2.2      10.2.2.2      bwYW1cg
  341        1.3tb     1.9tb    959.7gb      2.9tb           67 10.3.3.3      10.3.3.3      69O4sR-
  343        1.6tb     1.7tb      1.1tb      2.9tb           59 10.4.4.4      10.4.4.4      zCVUS05
  346        1.4tb     1.9tb   1000.6gb      2.9tb           66 10.5.5.5      10.5.5.5      AlvtEtf
  345        1.6tb     1.6tb      1.2tb      2.9tb           57 10.6.6.6      10.6.6.6      pmObkSm
  248          1tb       1tb    374.9gb      1.4tb           74 10.7.7.7      10.7.7.7      OSzB5J1
  332        1.4tb     1.9tb    952.1gb      2.9tb           68 10.8.8.8      10.8.8.8      EShhlrb
  218     1011.8gb       1tb    418.2gb      1.4tb           71 10.9.9.9      10.9.9.9      1rQ7PXD
  329        1.7tb     1.8tb        1tb      2.9tb           63 10.10.10.10   10.10.10.10   73TfUnn
   15                                                                                       UNASSIGNED  

這是我的環境中正在執行的 elasticsearch pod

$ echo "$output2"
NAME                              READY     STATUS    RESTARTS   AGE       IP              NODE
elasticsearch-4-40700             1/1       Running   0          4d        10.1.1.1        server1b.ocp.domain.com
elasticsearch-4-6m2p1             1/1       Running   0          4d        10.2.2.2        server2b.ocp.domain.com
elasticsearch-4-8n8v5             1/1       Running   0          7d        10.3.3.3        server3b.ocp.domain.com
elasticsearch-4-ffnk0             1/1       Running   0          4d        10.4.4.4        server4b.ocp.domain.com
elasticsearch-4-h5xr6             1/1       Running   0          7d        10.5.5.5        server5b.ocp.domain.com
elasticsearch-4-h6c15             1/1       Running   0          7d        10.6.6.6        server6b.ocp.domain.com
elasticsearch-4-j7cv5             1/1       Running   0          7d        10.7.7.7        server7b.ocp.domain.com
elasticsearch-4-nn3pc             1/1       Running   0          5d        10.8.8.8        server8b.ocp.domain.com
elasticsearch-4-sq5hr             1/1       Running   0          7d        10.9.9.9        server9b.ocp.domain.com
elasticsearch-4-zj7tq             1/1       Running   0          7d        10.10.10.10     server10b.ocp.domain.com
elasticsearch-4-az9sp             1/1       Running   0          7d        10.10.10.11     server11b.ocp.domain.com
elasticsearch-4-bi2c7             1/1       Running   0          7d        10.10.10.12     server12b.ocp.domain.com

根據 IP 地址從 output1 和 output2 執行“左外連接”後的預期結果

NAME                              READY     STATUS    RESTARTS   AGE       IP              NODE                      shards disk.indices disk.used disk.avail disk.total disk.percent node
elasticsearch-4-40700             1/1       Running   0          4d        10.1.1.1        server1b.ocp.domain.com      347        1.5tb     1.5tb      4.2tb      5.8tb           27 jxAuPMt 
elasticsearch-4-6m2p1             1/1       Running   0          4d        10.2.2.2        server2b.ocp.domain.com      335        1.7tb     1.7tb        4tb      5.8tb           30 bwYW1cg 
elasticsearch-4-8n8v5             1/1       Running   0          7d        10.3.3.3        server3b.ocp.domain.com      341        1.3tb     1.9tb    959.7gb      2.9tb           67 69O4sR- 
elasticsearch-4-ffnk0             1/1       Running   0          4d        10.4.4.4        server4b.ocp.domain.com      343        1.6tb     1.7tb      1.1tb      2.9tb           59 zCVUS05 
elasticsearch-4-h5xr6             1/1       Running   0          7d        10.5.5.5        server5b.ocp.domain.com      346        1.4tb     1.9tb   1000.6gb      2.9tb           66 AlvtEtf 
elasticsearch-4-h6c15             1/1       Running   0          7d        10.6.6.6        server6b.ocp.domain.com      345        1.6tb     1.6tb      1.2tb      2.9tb           57 pmObkSm 
elasticsearch-4-j7cv5             1/1       Running   0          7d        10.7.7.7        server7b.ocp.domain.com      248          1tb       1tb    374.9gb      1.4tb           74 OSzB5J1 
elasticsearch-4-nn3pc             1/1       Running   0          5d        10.8.8.8        server8b.ocp.domain.com      332        1.4tb     1.9tb    952.1gb      2.9tb           68 EShhlrb 
elasticsearch-4-sq5hr             1/1       Running   0          7d        10.9.9.9        server9b.ocp.domain.com      218     1011.8gb       1tb    418.2gb      1.4tb           71 1rQ7PXD 
elasticsearch-4-zj7tq             1/1       Running   0          7d        10.10.10.10     server10b.ocp.domain.com     329        1.7tb     1.8tb        1tb      2.9tb           63 73TfUnn 
elasticsearch-4-az9sp             1/1       Running   0          7d        10.10.10.11     server11b.ocp.domain.com      
elasticsearch-4-bi2c7             1/1       Running   0          7d        10.10.10.12     server12b.ocp.domain.com

下面產生您想要的輸出,但沒有標題:

awk 'NR==FNR{a[$8]=$0}FILENAME==ARGV[1]{b[$6]=$2} FILENAME==ARGV[2]{print $0,a[$6]}' <(oc get pod -lname=elasticsearch -o wide) <(curl -ks https://elasticsearch.apps.ocp.domain.com/_cat/allocation?v) | awk 'NR>1' | awk '!($14=$15="")'

有點不干淨的部分是如何刪除重複的 ip 地址列'awk '!($14=$15="")''

您可能最好使用一點多行腳本而不是單行來使此過程更清晰。

引用自:https://serverfault.com/questions/1032988