LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2012, 09:32 PM   #1
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 346

Rep: Reputation: 31
display 2nd part of variable in a shell script


ok, linux saavy, still need to sit in a cave for a week and learn sed/awk as it seems that's always the answer! Well I am remotely monitoring mysql, now want to graph the slave status. I have the following one line which works fine;

mysql -P 3306 -h host -e 'show slave status \G' | grep 'Seconds_Behind_Master'
Seconds_Behind_Master: 0

The monitoring app can take the value, graph it, I can trigger alarms, etc. so I need to only display the value, so I want to say just 0 (or whatever the value is. The Seconds_Behind_Master: is always exact so if I need to count characters and say after that is fine, or some other way, I am sure its simple for the right person/people, but I do want to get the scripting down more!

Tnx
 
Old 03-08-2012, 12:26 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well you can either put a second grep at the end:
Code:
mysql -P 3306 -h host -e 'show slave status \G' | grep 'Seconds_Behind_Master' | grep -Eo '[0-9]+$'
Or use sed to do something like:
Code:
mysql -P 3306 -h host -e 'show slave status \G' | sed -rn 's/Seconds_Behind_Master: ([0-9]+)/\1/p'
Or maybe with awk:
Code:
mysql -P 3306 -h host -e 'show slave status \G' | awk '/Seconds_Behind_Master/{print $NF}'
 
Old 03-08-2012, 09:14 PM   #3
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 346

Original Poster
Rep: Reputation: 31
[solved]

showoff ; )

Well the 1st grep I should have gotten first as I use grep alot but there are sooo many extra's. The other 2 I knew the command, but it's like jumping in the ocean & not even knowing how to swim outside the kiddie pool! That's the reason I do try to post, learn and thank those who help since it's the best way to learn I find vs. linux programming, page 1, echo 'hello world' !!

Thanks again!
 
Old 03-09-2012, 09:43 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
No probs ... Use the thread tools at the top of your post to mark as SOLVED.
 
  


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
Shell variable in awk script rohini_sharma Programming 3 02-22-2010 08:32 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
What is the best way to get a variable from another server with a shell script? abefroman Programming 2 01-29-2009 03:34 PM
shell script: redirect to variable va1damar Programming 1 02-05-2007 04:11 PM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:54 AM.

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