LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-28-2009, 12:30 PM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
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
 
Old 09-28-2009, 11:13 PM   #2
Smartpatrol
Member
 
Registered: Sep 2009
Posts: 196

Rep: Reputation: 38
...

Last edited by Smartpatrol; 03-11-2010 at 09:39 PM.
 
Old 10-01-2009, 03:49 PM   #3
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
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.
 
Old 10-01-2009, 03:59 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 10-02-2009, 09:21 PM   #5
Smartpatrol
Member
 
Registered: Sep 2009
Posts: 196

Rep: Reputation: 38
...

Last edited by Smartpatrol; 03-11-2010 at 09:39 PM.
 
Old 10-03-2009, 09:19 PM   #6
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thank you Forrestt for your input. Will take some time to study both of your inputs and apply to my script.
 
  


Reply



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
Alias command assistance to simplify norto Linux - Newbie 1 09-11-2008 01:55 AM
Problem executing a command with pipes DeepSeaNautilus Programming 12 08-08-2008 01:28 PM
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 12:10 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Write command in named pipes sahel Programming 7 12-28-2005 06:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:59 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
Open Source Consulting | Domain Registration