0

I have a source file like this:

server1 Mitigated Vulnerable Mitigated
server2 Vulnerable Vulnerable Mitigated
server3 Mitigated Vulnerable Vulnerable
server4 Mitigated Vulnerable Mitigated
server5 Mitigated Vulnerable Mitigated
server6 Vulnerable Vulnerable Vulnerable

I want to compare 2nd and 4th element of each row and add in a 5th element following this logic:

If either one ( 2nd or 4th element ) says vulnerable - 5th element will be Vulnerable ( and I want it printed in the same row like this:

server2 Vulnerable Vulnerable Mitigated Vulnerable

If only both ( 2nd and 4th ) are mitigated - Then 5th element will be mitigated

You can ignore the 3rd element.

1
  • server1 Mitigated Vulnerable Mitigated server2 Vulnerable Vulnerable Mitigated server3 Mitigated Vulnerable Vulnerable server4 Mitigated Vulnerable Mitigated server5 Mitigated Vulnerable Mitigated server6 Vulnerable Vulnerable Vulnerable
    – Param
    Jan 25, 2018 at 12:43

2 Answers 2

1

Using awk:

awk '$2 == "Vulnerable" || $4 == "Vulnerable" { $5 = "Vulnerable" } { print }' file

Result when using it on the example data:

server1 Mitigated Vulnerable Mitigated
server2 Vulnerable Vulnerable Mitigated Vulnerable
server3 Mitigated Vulnerable Vulnerable Vulnerable
server4 Mitigated Vulnerable Mitigated
server5 Mitigated Vulnerable Mitigated
server6 Vulnerable Vulnerable Vulnerable Vulnerable

The awk code looks at the second and fourth fields, and if any of these is exactly the string Vulnerable a fifth field is introduced. All fields (of all input lines) are then printed.

1

Awk solution:

For both conditions:

  • If either one ( 2nd or 4th element ) says vulnerable - 5th element will be Vulnerable
  • If only both ( 2nd and 4th ) are mitigated - Then 5th element will be Mitigated

awk '$2$4 ~ /[vV]ulnerable/{ $5="Vulnerable" }
     $2 ~ /[mM]itigated/ && $2 == $4{ $5="Mitigated" }1' file

The output:

server1 Mitigated Vulnerable Mitigated Mitigated
server2 Vulnerable Vulnerable Mitigated Vulnerable
server3 Mitigated Vulnerable Vulnerable Vulnerable
server4 Mitigated Vulnerable Mitigated Mitigated
server5 Mitigated Vulnerable Mitigated Mitigated
server6 Vulnerable Vulnerable Vulnerable Vulnerable
1
  • The 2nd response covers my entire scenario. Thanks for the 1st response - Yes it does cover the Vulnerable scenario like you had stated. I've used the 2nd awk response. I am still sorting out the output and will post anything else that comes up - But thank you so much guys for your prompt responses. - Param
    – Param
    Jan 25, 2018 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .