LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Simplify awk command by reducing pipes (https://www.linuxquestions.org/questions/linux-software-2/simplify-awk-command-by-reducing-pipes-758274/)

keysorsoze 09-28-2009 12:30 PM

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

Smartpatrol 09-28-2009 11:13 PM

...

keysorsoze 10-01-2009 03:49 PM

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.

forrestt 10-01-2009 03:59 PM

Using awk:
Code:

awk -F= '/hostcomment/,/}/ { if ($1 == "comment_id") { print $2 }}' status.dat
Using sed:

Code:

sed -ne '/hostcomment/,/}/ s/^comment_id=//p' status.dat
HTH

Forrest

Smartpatrol 10-02-2009 09:21 PM

...

keysorsoze 10-03-2009 09:19 PM

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.