Simplify awk command by reducing pipes
Hello guys, I came up with the following way of stringing up a bunch of awk commands to get what I needed which was a field of numbers but was wondering if there was a more efficient way some members could see accomplishing this.
awk '/hostcomment/,/}/' /var/nagios/status.dat | awk '/comment_id/' | awk -F= '{print $2}' I get the task done but I always wonder if this is the best and most efficient. Thanks |
...
|
Hi Smartpatrol, here is the data:
hostcomment { host_name=localhost entry_type=1 comment_id=36 source=1 persistent=1 entry_time=1254238506 expires=0 expire_time=0 author=Nagios Admin comment_data=blah blah blah } [root@nagios_lga admin.dk]# awk '/hostcomment/,/}/' /var/nagios/status.dat hostcomment { host_name=localhost entry_type=1 comment_id=36 source=1 persistent=1 entry_time=1254238506 expires=0 expire_time=0 author=Nagios Admin comment_data=blah blah blah } hostcomment { host_name=localhost entry_type=1 comment_id=37 source=1 persistent=1 entry_time=1254238516 expires=0 expire_time=0 author=Nagios Admin comment_data=blah blah blah 2 } My goal was to awk out the comment_id field. I understand I could have just did a grep for "comment_id" but there was mixed service and host comments in this file. |
Using awk:
Code:
awk -F= '/hostcomment/,/}/ { if ($1 == "comment_id") { print $2 }}' status.dat Code:
sed -ne '/hostcomment/,/}/ s/^comment_id=//p' status.dat Forrest |
...
|
Thank you Forrestt for your input. Will take some time to study both of your inputs and apply to my script.
|
All times are GMT -5. The time now is 09:46 PM. |