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! |
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 |
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 |
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. |
My bad ... I forgot one crucial bit:
Code:
awk -F, '$2 ~ /BBB 9.0/ || $3 ~ /CCC x87h/' file |
Try this.
Code:
BEGIN{} Code:
{ Code:
cat data_filename | awk -f this_script.awk |
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 |
Should be
Code:
awk -F, '$2 ~ /BBB 9.0/ && $3 ~ /CCC x87h/' file |
@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!! |
It is not a comparison between // and && but rather || and &&.
|| - OR && - AND |
Ah, I see,
thanks guys! |
All times are GMT -5. The time now is 11:19 PM. |