Home > Software design >  Match Column and Line in Shell Script
Match Column and Line in Shell Script

Time:12-09

Input File-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

Expected Output

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

Code

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

I want to get the column that has only Failed values or they haven't got completed in any of the rows.

CodePudding user response:

If awk is your option, would you please try:

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

Output:

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