LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-02-2006, 06:38 PM   #1
huynguye
LQ Newbie
 
Registered: May 2006
Posts: 3

Rep: Reputation: 0
awk print lines that doesn't have a pattern


Hi,

I'm looking for a way to print out lines that do not have the words Mozilla, Opera or JanusUtils.

So far I can print the lines that have Mozilla, but I am unsure how to do this for multiple words and to not print them instead of printing them.

Code:
awk '/Mozilla/ {print}'
Any help is appreciated. Thanks,
Huy
 
Old 05-02-2006, 08:39 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
If you're not tied to using Awk, GNU Grep can do it:
Code:
grep -Ev 'Mozilla|Opera|JanusUtils'
The -v option means show lines that don't match, -E means use extended regular expressions (seems to be required to use the pipe [which means "or"] correctly).
 
Old 05-03-2006, 02:29 PM   #3
huynguye
LQ Newbie
 
Registered: May 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by taylor_venable
If you're not tied to using Awk, GNU Grep can do it:
Code:
grep -Ev 'Mozilla|Opera|JanusUtils'
The -v option means show lines that don't match, -E means use extended regular expressions (seems to be required to use the pipe [which means "or"] correctly).
That works, but unfortunately I am tied to using Awk. From your example, I was able to expand to this now
Code:
awk '/Mozilla|Opera|JanusUtils/ {printf "%s\t%s\t%s\n",$12,$4,$1 }'
There must be a way to not print those finds. =/
 
Old 05-03-2006, 08:58 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
OK, in that case, you can use an "if" statement. This file will check for (the lack of) a match on variable $1:
Code:
{
    if ( $1 !~ /Mozilla|Opera|JanusUtils/ ) {
        print $1;
    }
}
It's a little less flexible than being able to match the entire line, but if what you're looking for can only occur in one column, it should work. Otherwise... I'd have to dig through my Awk book some more.
 
Old 05-04-2006, 01:59 AM   #5
huynguye
LQ Newbie
 
Registered: May 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks that worked and made me think of another possible solution.

Code:
awk '!/Mozilla|Opera|JanusUtils/ {printf "%s\t%s\t%s\n",$12,$4,$1 }'
I tried adding the ! and it worked!! I should've thought of that earlier =P Thanks for the inspiration!
 
Old 05-04-2006, 11:08 AM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Cool; I had no idea that feature existed. I guess it just shows that the best way to learn around Unix is through experimentation.
 
  


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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
awk text that is on several lines homey Programming 2 10-31-2004 09:27 AM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM


All times are GMT -5. The time now is 10:13 AM.

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