匹配 Shell 脚本中的列和行

Match Column and Line in Shell Script

输入文件-test1

Failed ,abc,  /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,abc, /clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Completed, abc /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,def ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ,ghi  ,/clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed ,jkl ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Completed ,def ,/clients/FORD_1030PM_EST_Windows2008, Windows File System
Completed ,hkm ,/clients/FORD_1030PM_EST_Windows2008 Windows File System

预期输出

Failed ghi,  /clients/FORD_1030PM_EST_Windows2008, Windows File System
Failed jkl, /clients/FORD_1030PM_EST_Windows2008, Windows File System

代码

sed -n '/Completed/ s,\(.*\) .* Completed$,,a' "$pwd"/test1 | grep -v -f - "$pwd"/test1

我想获取只有失败值或它们在任何行中都没有完成的列。

如果awk是您的选择,请您尝试:

awk '
/^Failed/ {gsub(/,/, "", ); fail[]=[=10=]}         # if failed, store the line
/^Completed/ {gsub(/,/, "", ); delete fail[]}  # if completed, abandon the line from the list
END {for (i in fail) print fail[i]}     # finally print the remaining list
' file

输出:

Failed jkl ,/clients/FORD_1030PM_EST_Windows2008 ,Windows File System
Failed ghi ,/clients/FORD_1030PM_EST_Windows2008, Windows File System