Awk

從輸出 bash 更改某些行的顏色

  • April 18, 2022

我有來自驅動器診斷命令的輸出。

Slot Number        : 0   
Drive's position   : DiskGroup 0  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 447.130 GB [0x37e436b0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : PHYM813201FL480BGNSSDSC2KG480G7R SCV1DL58  
Foreign State      : None   
Media Type         : Solid State Device 
Temperature        : 20C (68.00 F) 
S.M.A.R.T alert    : No   

Slot Number        : 1   
Drive's position   : DiskGroup 0  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 447.130 GB [0x37e436b0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : PHYM81320058480BGNSSDSC2KG480G7R SCV1DL58  
Foreign State      : None   
Media Type         : Solid State Device 
Temperature        : 21C (69.80 F) 
S.M.A.R.T alert    : No   

Slot Number        : 2   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0VEBK 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No   

Slot Number        : 3   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0T07T 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No   

Slot Number        : 4   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0VAJK 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No   

Slot Number        : 5   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0SYPC 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No   

Slot Number        : 6   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0MVN2 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No   

Slot Number        : 7   
Drive's position   : DiskGroup 1  
Media Error        : 0   
Other Error        : 0   
Predictive Failure : 0   
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Online, Spun Up 
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0SZ2L 
Foreign State      : None   
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : No 

我只想在“:”符號後的每個驅動器的第 3、4、5、7、9 和 12 行上進行更改。這些結果是標準,但如果它說的不同,我想添加一些顏色。例如:

Slot Number        : 7   
Drive's position   : DiskGroup 1  
Media Error        : 4  (I want the whole line in red)
Other Error        : 3  (I want the whole line in red)
Predictive Failure : 1  (I want the whole line in red)
Raw Size           : 1.090 TB [0x8bba0cb0 Sectors]
Firmware state     : Failed. (I want the whole line in red)
Inquiry Data       : SEAGATE ST1200MM0099 ST31WFK0SZ2L 
Foreign State      : Unconfigured  (I want the whole line in red)
Media Type         : Hard Disk Device 
Temperature        : 22C (71.60 F) 
S.M.A.R.T alert    : Yes  (I want the whole line in red)

我想對每個驅動器進行這些更改,驅動器數量是可變的,有時更多有時更少。

我已經嘗試過了,但是沒有用。

awk -F ':' '$1~/^(Media Error|Other Error|Predictive Failure)$/ && $2 > 0 {print "\033[31m" $0 "\e[0m"; next} 1'

你已經非常接近正確了。我會用 Bash 和 Awk 來回應。你似乎要求的是:

$ awk '/(Media Error|Other Error|Predictive Failure|Firmware state|Foreign State|S\.M\.A\.R\.T alert)[[:blank:]]+:/{print "\033[31m" $0"\033[0m"; next} 1' < <(your_drive_diag_command)

您在發布之前所做的嘗試值得一些評論:

  • -F ':'
    僅當您確實需要對記錄的欄位進行操作時,指定不同於預設空格的欄位分隔符才有用。這裡似乎並非如此。
  • $1 ~ /^(Media Error|Other Error|Predictive Failure)$/ ...
    您使用(OR) 布爾值根據字元串值測試第一個欄位($1在指定欄位分隔符之前:)的 3 種不同可能性。|原則上這沒問題,但在這種情況下,您可能只需測試整個記錄 ( $0) 中任何位置的字元串是否存在,只需確保您使用\s+:Gawk 或[[:blank:]]+:任何 Awk 風格完成所尋求的字元串正則表達式即可。:這會在每個查找的字元串的冒號前添加多個空格/製表符 >= 1 。換句話說,您可能可以取消使用$1包含^and的正則表達式進行測試$
  • 我對齊了您的顏色編碼以保持一致性,因此\e變為\033.

編輯 1:

遵循@JosueBaquerizo 的附加規範,邏輯更改為在$1and$2

$ awk -F ' *: *' '
     ($1 ~ /Media Error|Other Error|Predictive Failure/ && $2 > 0 ||
      $1 ~ /Firmware state/ && $2 ~ "Failed" ||
      $1 ~ /Foreign State/ && $2 ~ "Unconfigured" || 
      $1 ~ /S\.M\.A\.R\.T alert/ && $2 ~ "Yes") {print "\033[31m" $0"\033[0m"; next} 1' < <(your_drive_diag_command)

引用自:https://unix.stackexchange.com/questions/698559