匹配列并删除 Shell 中的重复项
match column and delete the Duplicates in Shell
输入文件
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 ',' '
~/Failed/ { fail[]=[=13=] }
~/Completed/ {delete fail[]}
~ /Demand/ {delete fail[]}
END {for (i in fail) print fail[i]}
' test
您可以使用这个 awk
命令:
awk -F, 'NR==FNR {if (~/Failed/) fail[] = [=10=]; next}
~ /Completed/ || ~ /Demand/ {delete fail[]}
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
使用您显示的示例,请尝试执行以下 awk
程序。在 Input_file 的单次传递中。这将仅打印那些失败的值,并且根据显示的示例,它们的值永远不会有任何按需。
awk '
BEGIN { FS=OFS="," }
=="Failed" { arr1[]=[=10=] }
~/On-Demand/{ arr2[] }
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.
=="Failed" { arr1[]=[=11=] } ##Checking if 1st field is Failed then create arr1 with 4th field as an index and value of whole line.
~/On-Demand/{ arr2[] } ##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.
这是一个 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
输入文件
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 ',' '
~/Failed/ { fail[]=[=13=] }
~/Completed/ {delete fail[]}
~ /Demand/ {delete fail[]}
END {for (i in fail) print fail[i]}
' test
您可以使用这个 awk
命令:
awk -F, 'NR==FNR {if (~/Failed/) fail[] = [=10=]; next}
~ /Completed/ || ~ /Demand/ {delete fail[]}
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
使用您显示的示例,请尝试执行以下 awk
程序。在 Input_file 的单次传递中。这将仅打印那些失败的值,并且根据显示的示例,它们的值永远不会有任何按需。
awk '
BEGIN { FS=OFS="," }
=="Failed" { arr1[]=[=10=] }
~/On-Demand/{ arr2[] }
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.
=="Failed" { arr1[]=[=11=] } ##Checking if 1st field is Failed then create arr1 with 4th field as an index and value of whole line.
~/On-Demand/{ arr2[] } ##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.
这是一个 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