LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2011, 06:45 PM   #1
Guilty1682
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
Awk command help


Hi guys,
I'm having a little of trouble with awk.
Assuming I have a table or layout as the following:

Environment OS Version
AAA, BBB 9.0, CCC x87h
CCC, DDD 7.1, HHH g35p
AAA, BBB 6.1, CCC x87h
AAA, BBB 9.0, CCC x87h
CCC, DDD 2.1, HHH z93y
AAA, BBB 9.0, CCC x87h
AAA, BBB 9.0, CCC r61v
AAA, BBB 9.0, CCC k90s

I want to have an output that will display only Environment that CONTAINS BBB 9.0
and CCC x87h

How do I do that? Should I use grep in this situation?

Thanks!
 
Old 02-21-2011, 06:52 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

If position of the match is relevant awk is a good choice:
Code:
awk '$2 ~ /BBB 9.0/ || $3 ~ /CCC x87h/' file


Cheers,
Tink

Last edited by Tinkster; 02-21-2011 at 07:23 PM.
 
Old 02-21-2011, 06:53 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
I'm a bit rusty but something like this in an awk script.

BEGIN {}
if(( $2 ~ /BBB/ ) && ( $3 ~ /9\.0/)) {
print $0;
}
END{}

Then you would cat file | awk -f script.awk

Last edited by trist007; 02-21-2011 at 07:11 PM.
 
Old 02-21-2011, 07:04 PM   #4
Guilty1682
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks both you!!

However, it did not work for me, I was using this command --> awk '$2 ~ /BBB 9.0/ || $3 ~ /CCC x87h/' file
after I typed mine up, it did not have any output. Basically, nothing happens.
I'm pretty sure it's something I did xD
But, do you mind explain to me about your command? like break it down a bit? So, I can have a clear understanding of it?

Thanks!

P.S. I just saw my original post, the format is messed up. The envionment, OS, Version are the title and below them are the layout,
they are also separated by "," to match up with the title.
 
Old 02-21-2011, 07:24 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
My bad ... I forgot one crucial bit:
Code:
awk -F, '$2 ~ /BBB 9.0/ || $3 ~ /CCC x87h/' file
 
Old 02-21-2011, 07:26 PM   #6
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
Try this.
Code:
BEGIN{}
{
if(/BBB 9\.0/ && /CCC x87h/)
          print $0;
}
END{}
or ultra thin version
Code:
{
if(/BBB 9\.0/ && /CCC x87h/)
          print $0;
}
Then do
Code:
cat data_filename | awk -f this_script.awk

Last edited by trist007; 02-21-2011 at 07:37 PM.
 
Old 02-21-2011, 07:52 PM   #7
Guilty1682
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Sweet! Thanks guys! both methods worked like a charm!
I have some output now.

However, as for the
awk -F, '$2 ~ /BBB 9.0/ || $3 ~ /CCC x87h/' file

There are some other output got mixed up with "good" display.
Ex.
AAA1, BBB 9.0, CCC x87h
AAA2, BBB 9.0, CCC x87h
AAA3, BBB 9.0, CCC x87h
AAA4, BBB 5.6, CCC x87h
AAA5, BBB 9.0, CCC v73k


Any idea why is it like that?

Thank you so much!

Also, I totally agree with you for I'll rather attempt to aid you in helping yourself than
giving you a "turn-key solution"

I did some research and right now I have a pretty good understand of what you did.

Last edited by Guilty1682; 02-21-2011 at 07:55 PM.
 
Old 02-21-2011, 07:57 PM   #8
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,027

Rep: Reputation: 69
Should be
Code:
awk -F, '$2 ~ /BBB 9.0/ && $3 ~ /CCC x87h/' file
 
Old 02-21-2011, 08:00 PM   #9
Guilty1682
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
@Trist007
Thank you! that worked perfectly, what's the difference between // and &&? I look through the websites around a bit, my only conclusion is that they both serve as "AND" if I'm not mistaken.

If I'm wrong, please point me to the right direction.

Thanks again both of you, it was great help!!
 
Old 02-21-2011, 08:30 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
It is not a comparison between // and && but rather || and &&.

|| - OR
&& - AND
 
Old 02-21-2011, 09:24 PM   #11
Guilty1682
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Ah, I see,
thanks guys!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk command help fredora Programming 4 11-25-2010 06:23 PM
About awk command incomingid Linux - Newbie 5 04-01-2009 11:44 PM
awk command toaravind Linux - General 5 07-02-2008 02:25 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
the 'awk' command. iconicmoronic Linux - Newbie 2 04-08-2007 01:29 AM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration