輸入檔案
Failed,2021-12-14 05:47 EST,On-Demand Backup,abc,/clients/FORD_1130PM_EST_Windows2008,Windows File System
Completed,2021-12-14 05:47 EST,On-Demand Backup,def,/clients/FORD_1130PM_EST_Windows2008,Windows File System
Failed,2021-12-13 19:33 EST,Scheduled Backup,def,/clients/FORD_730PM_EST_Windows2008,Windows File System
Failed,2021-12-14 00:09 EST,Scheduled Backup,abc,/clients/FORD_1130PM_EST_Windows2008,Windows File System
Failed,2021-12-14 00:09 EST,Scheduled Backup,ghi,/clients/FORD_1130PM_EST_Windows2008,Windows File System
預期產出
Failed,2021-12-14 00:09 EST,Scheduled Backup,ghi,/clients/FORD_1130PM_EST_Windows2008,Windows File System
我只想要那些永遠不會成功并且沒有按需備份運行的客戶端。
我試過的代碼
awk -F ',' '
$1~/Failed/ { fail[$4]=$0 }
$1~/Completed/ {delete fail[$4]}
$3 ~ /Demand/ {delete fail[$4]}
END {for (i in fail) print fail[i]}
' test
uj5u.com熱心網友回復:
你可以使用這個awk命令:
awk -F, 'NR==FNR {if ($1~/Failed/) fail[$4] = $0; next}
$1 ~ /Completed/ || $3 ~ /Demand/ {delete fail[$4]}
END {for (i in fail) print fail[i]}' file file
Failed,2021-12-14 00:09 EST,Scheduled Backup,ghi,/clients/FORD_1130PM_EST_Windows2008,Windows File System
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下awk程式。在 Input_file 的單次傳遞中。這將僅列印那些失敗的值,并且根據顯示的示例在其值中永遠不會有任何按需值。
awk '
BEGIN { FS=OFS="," }
$1=="Failed" { arr1[$4]=$0 }
$3~/On-Demand/{ arr2[$4] }
END{
for(key in arr1){
if(!(key in arr2)){
print arr1[key]
}
}
}
' Input_file
說明:為以上添加詳細說明。
awk ' ##Starting awk program from here.
BEGIN { FS=OFS="," } ##Starting BEGIN section and setting FS and OFS to , here.
$1=="Failed" { arr1[$4]=$0 } ##Checking if 1st field is Failed then create arr1 with 4th field as an index and value of whole line.
$3~/On-Demand/{ arr2[$4] } ##Checking if 3rd field is On-Demand then create arr2 array with index of 4th field.
END{ ##Starting END block of this program from here.
for(key in arr1){ ##Traversing through arr1 here.
if(!(key in arr2)){ ##Checking condition if key is NOT present in arr2 then do following.
print arr1[key] ##Printing arr1 value with index of key here.
}
}
}
' Input_file ##Mentioning Input_file here.
uj5u.com熱心網友回復:
這是一個 ruby??,它將處理多個條目(如果有)和 csv 怪癖,例如嵌入的逗號:
ruby -r csv -e '
BEGIN{hsh = Hash.new {|hash,key| hash[key] = []}
data = Hash.new {|hash,key| hash[key] = []}
}
CSV.parse($<.read).each{ |r| hsh[r[3]] << r[0]; hsh[r[3]] << r[2]
data[r[3]] << r.to_csv
}
END{hsh.each{|k,v| s=v.join("\t")
puts data[k].join() if !s[/Completed|Demand/] }
}' file
印刷:
Failed,2021-12-14 00:09 EST,Scheduled Backup,ghi,/clients/FORD_1130PM_EST_Windows2008,Windows File System
uj5u.com熱心網友回復:
$ egrep "failed|On-Demand Backup" <filename>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382278.html
上一篇:帶有嵌套for回圈的Javascript多維陣列-無法正常作業
下一篇:從CSV讀取的奇怪問題
