LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk command help (https://www.linuxquestions.org/questions/linux-newbie-8/awk-command-help-864158/)

Guilty1682 02-21-2011 05:45 PM

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!

Tinkster 02-21-2011 05:52 PM

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

trist007 02-21-2011 05:53 PM

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

Guilty1682 02-21-2011 06:04 PM

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.

Tinkster 02-21-2011 06:24 PM

My bad ... I forgot one crucial bit:
Code:

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

trist007 02-21-2011 06:26 PM

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

Guilty1682 02-21-2011 06:52 PM

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" :D

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

trist007 02-21-2011 06:57 PM

Should be
Code:

awk -F, '$2 ~ /BBB 9.0/ && $3 ~ /CCC x87h/' file

Guilty1682 02-21-2011 07:00 PM

@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!!

grail 02-21-2011 07:30 PM

It is not a comparison between // and && but rather || and &&.

|| - OR
&& - AND

Guilty1682 02-21-2011 08:24 PM

Ah, I see,
thanks guys!


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