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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-09-2011, 08:21 AM
|
#1
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Rep: 
|
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!!!
|
|
|
|
09-09-2011, 09:14 AM
|
#2
|
|
Member
Registered: Aug 2009
Distribution: CentOS
Posts: 692
|
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.
|
09-09-2011, 09:27 AM
|
#3
|
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,314
|
It should work to negate the expression, either completely or:
Code:
$ awk '$1 !~ "AA" || $4 ~ "BB"'
|
|
|
|
09-09-2011, 10:08 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,317
|
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?
|
|
|
|
09-09-2011, 10:37 AM
|
#5
|
|
Senior Member
Registered: Jan 2010
Posts: 1,604
|
boolean algebra
Quote:
Originally Posted by grail
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:
|
|
|
1 members found this post helpful.
|
09-09-2011, 12:40 PM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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.
|
|
|
|
09-12-2011, 05:00 AM
|
#7
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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?
|
|
|
|
09-12-2011, 05:12 AM
|
#8
|
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,314
|
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.
|
09-12-2011, 06:51 AM
|
#9
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by crts
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:
|
Now I understand, it is a really logic problem. Thanks a lot for your detailed and clear explanation crts!!Really helpful!!
|
|
|
|
09-12-2011, 06:55 AM
|
#10
|
|
Member
Registered: Aug 2011
Distribution: Ubuntu
Posts: 96
Original Poster
Rep: 
|
Quote:
Originally Posted by Reuti
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!!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:58 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|