Awk + single-line awk syntax to print only after the second field if matching is true

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

eg

I have a 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 

awk will only print the following:

  I_am_very_tall my_ball_is_fine 

Since I_am_very_tall, my_ball_is_fine gets true in the first field without false

my_dog_is_fat, my_name_is_lenon is not printed because the false word in the first field

The rule is to print the second field if there was no false word in the first field! (From all the same sentence in the second field)

Lidia

+4
source share
7 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 several 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 
+4
source

Assuming each block has the same category.

 $ awk -vRS= '!/false/' file | uniq | awk '{print $NF}' I_am_very_tall my_ball_is_fine 
+1
source

I'm not sure if this is possible as a single line awk. At least not easy. Is awk required? Since there is one shell shell solution, if awk is only part of the solution.

For example, this is a single-layer shell (only a long line):

 awk '{ print $2 }' < {infile} | while read line; do grep -i "false $line" {infile} > /dev/null || echo $line; done | uniq 

It is inefficient, but it does the job.

For comparison, the shortest awk solution I have is about 25 lines (which I can here, if you want, leave a comment and I will update this accordingly). But that would mean that you could save this to a file and run awk -f alltrue.awk < {infile} . I'm not sure if these are strictly classes like perl one-liner though :-)

A better understanding of what you end up trying to achieve or what it is may be helpful.

0
source

And one more:

 awk 'END { for (A in all) if (!(A in ko)) print A } NF { all[$2] } $1 == "false" { ko[$2] } ' infile 

If you want to keep the original order, you need more code.

0
source

Another compact solution.

 awk ' $1 == "true" && ! ($2 in false) {true[$2]} $1 == "false" {false[$2]; delete true[$2]} END {for (word in true) print word} ' true.txt 
0
source

Here is another way:

 gawk '{a[$2]=a[$2] || $1=="false"} END {for (i in a) if (!a[i] && i) print i}' a.txt my_ball_is_fine I_am_very_tall 
0
source

Here is one liner:

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

my_ball_is_fine I_am_very_tall

0
source

Source: https://habr.com/ru/post/1316641/


All Articles