LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk achieve "do not print" (https://www.linuxquestions.org/questions/programming-9/awk-achieve-do-not-print-902126/)

cristalp 09-09-2011 08:21 AM

awk achieve "do not print"
 
I would like to delete lines match specific pattern. How can I do something like:

Code:


awk `$1 ~ "AA" && $4 !~ "BB" { do not print these lines}`

I can not simply print the other lines by match the patters in them. Because the others lines are not regular, with different fields and without special patter to match. It is more difficult to print other lines than do not print these lines with $1 match AA and $4 not match BB.

How could I do this?

Many thanks!!!

rknichols 09-09-2011 09:14 AM

Use the next command, which stops processing of the current line. Follow that with a pattern-action statement with an empty pattern section (matches all lines) and an action that is simply "print".
Code:

awk '$1 ~ "AA" && $4 !~ "BB" {next} {print}'

Reuti 09-09-2011 09:27 AM

It should work to negate the expression, either completely or:
Code:

$ awk '$1 !~ "AA" || $4 ~ "BB"'

grail 09-09-2011 10:08 AM

I think Reuti is on the right path but shouldn't it be && and not ||, ie. you want both things to be true don't you?

crts 09-09-2011 10:37 AM

boolean algebra
 
Quote:

Originally Posted by grail (Post 4467390)
I think Reuti is on the right path but shouldn't it be && and not ||, ie. you want both things to be true don't you?

Actually, reuti's and rknichols solutions are equivalent. The only difference is that rknichols solution does not print when the condition is true and reuti's solution only prints when the condition is true. Therefor reuti uses the negated expression of rknichols.
Code:

rknichols:
(a && (!b)) -> do not print

reuti:
! (a && (!b)) -> print

With the rules for boolean algebra the latter can be transformed to:
Code:

(!a) || b -> print

David the H. 09-09-2011 12:40 PM

By the way, the backticks in the OP example would be read as a command substitution. Be careful not to confuse them with regular single quotes.

http://mywiki.wooledge.org/Quotes

And of course $(..) is highly recommended over `..` for command substitution anyway.

cristalp 09-12-2011 05:00 AM

Quote:

Originally Posted by grail (Post 4467390)
I think Reuti is on the right path but shouldn't it be && and not ||, ie. you want both things to be true don't you?

Thanks grail! What I want to is not printing 1st field with string match AA AND 4th field with sting NOT match BB. Print all the other lines. I tried Reuti's code. It does not work and I do not quite understand the logic behind it. If you would have a better understanding, could you please give a better explanation?

Reuti 09-12-2011 05:12 AM

Hi,

crts explained exactly what I had in mind by using rules to simplify the expression.

I get:
Code:

$ cat file
11 22 33 44
AA 22 33 44
11 22 33 BB
AA 22 33 BB
$ awk '$1 !~ "AA" || $4 ~ "BB"' file
11 22 33 44
11 22 33 BB
AA 22 33 BB

So, if $1 is AA and $4 is not BB, it’s not printed. What lines do you expect to appear in the output of the four cases?

cristalp 09-12-2011 06:51 AM

Quote:

Originally Posted by crts (Post 4467416)
Actually, reuti's and rknichols solutions are equivalent. The only difference is that rknichols solution does not print when the condition is true and reuti's solution only prints when the condition is true. Therefor reuti uses the negated expression of rknichols.
Code:

rknichols:
(a && (!b)) -> do not print

reuti:
! (a && (!b)) -> print

With the rules for boolean algebra the latter can be transformed to:
Code:

(!a) || b -> print


Now I understand, it is a really logic problem. Thanks a lot for your detailed and clear explanation crts!!Really helpful!!

cristalp 09-12-2011 06:55 AM

Quote:

Originally Posted by Reuti (Post 4469567)
Hi,

crts explained exactly what I had in mind by using rules to simplify the expression.

I get:
Code:

$ cat file
11 22 33 44
AA 22 33 44
11 22 33 BB
AA 22 33 BB
$ awk '$1 !~ "AA" || $4 ~ "BB"' file
11 22 33 44
11 22 33 BB
AA 22 33 BB

So, if $1 is AA and $4 is not BB, it’s not printed. What lines do you expect to appear in the output of the four cases?

Sorry Reuti, I guess I made something wrong last time when I tested your code. I did it again now and it was proved to be correct. You are in the right logic with shorter code. You teach me a lot. Thanks!!


All times are GMT -5. The time now is 06:06 PM.