Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk + one line awk syntax to print only once the second field if "true" word matched

Tags:

awk

I don't know how to do this with awk But my target is to create awk one line syntax in order to print the second field ($2) if all first field ($1) are true

for example

I have the file:

   true my_name_is_lenon
   true my_name_is_lenon
   true my_name_is_lenon
   false my_name_is_lenon
   true my_name_is_lenon

   false my_dog_is_fat
   true my_dog_is_fat
   true my_dog_is_fat

   true I_am_very_tall
   true I_am_very_tall
   true I_am_very_tall

   true my_ball_is_fine
   true my_ball_is_fine

the awk will print only the following:

 I_am_very_tall
 my_ball_is_fine

Because I_am_very_tall , my_ball_is_fine get true on the first field without false

my_dog_is_fat , my_name_is_lenon not printed because the false word on the first field

The rule is to print the second field if no false word on the first field! (Of all the same sentence on the second field)

lidia

like image 714
lidia Avatar asked Feb 27 '23 14:02

lidia


1 Answers

Here is one way:

awk '{if ($1 == "false") {array[$2] = $1} else if (array[$2] != "false") array[$2] = $1} END {for (i in array) if (array[i] == "true") print i}' inputfile

Edit:

Here it is on multiple lines:

awk '{if ($1 == "false") 
         {array[$2] = $1} 
     else if (array[$2] != "false") 
         array[$2] = $1} 
     END {for (i in array)
             if (array[i] == "true") 
                 print i}
' inputfile
like image 74
Dennis Williamson Avatar answered May 15 '23 02:05

Dennis Williamson