LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-09-2011, 08:21 AM   #1
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Rep: Reputation: Disabled
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!!!
 
Old 09-09-2011, 09:14 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,157

Rep: Reputation: 493Reputation: 493Reputation: 493Reputation: 493Reputation: 493
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}'
 
1 members found this post helpful.
Old 09-09-2011, 09:27 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
It should work to negate the expression, either completely or:
Code:
$ awk '$1 !~ "AA" || $4 ~ "BB"'
 
Old 09-09-2011, 10:08 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,185

Rep: Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782Reputation: 1782
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?
 
Old 09-09-2011, 10:37 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
boolean algebra

Quote:
Originally Posted by grail View Post
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
 
1 members found this post helpful.
Old 09-09-2011, 12:40 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
Old 09-12-2011, 05:00 AM   #7
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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?
 
Old 09-12-2011, 05:12 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
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?
 
1 members found this post helpful.
Old 09-12-2011, 06:51 AM   #9
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by crts View Post
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!!
 
Old 09-12-2011, 06:55 AM   #10
cristalp
Member
 
Registered: Aug 2011
Distribution: Linux Mint
Posts: 103

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Reuti View Post
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!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I pass variable to "awk" and use print ? 915086731 Linux - Software 11 08-11-2011 09:35 PM
[SOLVED] Perl: Condition?print "Is true":print "Is false"; angel115 Programming 2 09-17-2010 06:28 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
How do you do ps ax | grep mozilla | awk '{print "kill " $1}' pppaaarrrkkk Programming 4 03-19-2008 07:17 PM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration