LinuxQuestions.org
Help answer threads with 0 replies.
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 02-26-2008, 11:28 AM   #1
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Rep: Reputation: 15
Help with Bash (grep/awk/etc)


Hey everyone,

I want to write a script which can parse data I get from a java tool. Basically the tool prints out a number of lines, but I'm only interested in the last line, which basically gives the status of a server. I'd like to parse this but I'm not certain what tools I should use.

Take the following example output of the tool:

Code:
./status.sh SERVER1
... lots of junk ...
00I: The server "SERVER1" is STARTED

./status.sh SERVER1
... lots of junk ...
00I: The server "SERVER1" is STARTING

./status.sh SERVER1
... lots of junk ...
09I: The server "SERVER1" cannot be reached. It appears to be stopped.
What I'd like to do, is to be able to operate based on the data returned from the "status.sh" script. If someone could recommend what tools would be best for this job I'd really appreciate it (grep -E ? awk?). I don't need any source code (although pseudo code would be welcome ).

Cheers,
 
Old 02-26-2008, 11:31 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
Hi.

'tail -n 1' will give just the last line. You can grep from there I'd imagine, like:
Code:
if ./status.sh SERVER1 | tail -n1 | grep "cannot be reached"
    then
    echo "Noooooooooooooooooooooooooo."
fi
Dave
 
Old 02-26-2008, 11:45 AM   #3
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Hi, that was quick

I probably should of said I don't really have an issue with getting the last line, its more a case of getting the status itself that's confusing me. Since I need to check for the different kinds of status.

Some pseudo code of what I'm trying to do would look like this:
Code:
populate a variable "STATUS" from status.sh

case $STATUS in
STARTED)
 echo "...";;
STOPPED)
 echo "...";;
STARTING)
 echo "...";;
esac
The problem I have is with populating that STATUS variable. Perhaps it's just the way I'm thinking about it that's confusing me and this can be solved in an easier way.

Thanks again,
 
Old 02-26-2008, 12:38 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by piercey View Post
The problem I have is with populating that STATUS variable. Perhaps it's just the way I'm thinking about it that's confusing me and this can be solved in an easier way.
So the format is always the same (the status is any text after ‘is’ on the last line)? In that case, what about the following:
Code:
STATUS=$( ./status.sh SERVER1 | sed -ne '${s/^.*is //;p}' )
 
Old 02-26-2008, 12:40 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
If you mean you want the last word in a given line, you can get it with cut, which should be faster than awk. Grep only handles lines, so that wouldn't be what you want.

:echo '00I: The server "SERVER1" is STARTING' | cut -d' ' -f6
STARTING

If you want to perform an action based on status, you may be able to use the exit status.

:if ls nodir 2>/dev/null; then echo exists; else echo noexist; fi
noexist

You can also capture the specific code with $?.

If none of this helps, maybe restate the problem more precisely.
 
Old 02-26-2008, 01:52 PM   #6
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks for the replies guys.

osor/digiot: Both methods work fine. Only slight problem is that for the server when it's stopped the last word is "stopped.". Not exactly a significant problem, just feels a little hackish (case's would be "STARTED", "STARTING" and "stopped.") I'm really just doing this as a learning experience so I'm not in any rush to get this completed. I'm more interested in ways to solve it.

Quote:
If you want to perform an action based on status, you may be able to use the exit status.
This could be worthwhile. The problem is status.sh basically just sets some environment variables and runs a java application, I've no idea if it will respect exit status', so I didn't even try. I'll be sure to try it tomorrow when I'm at the machine again.

Cheers
 
Old 02-26-2008, 05:29 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, you could check for eack keyword (assuming each always appears on it's own)

Code:
result=`./status.sh|tail -1`
echo $result|grep stopped >dev/null
if [[ $? -eq 0 ]]
then
    echo "Server STOPPED"
fi
echo $result|grep START >dev/null
if [[ $? -eq 0 ]]
then
    echo "Server STARTED/ING"
fi
 
Old 02-26-2008, 06:56 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
./status.h SERVER1 | awk 'END{
 pattern="STARTED|STARTING|STOPPED"
 if ( $0 ~ pattern) {
   print $0
 }
}'
 
Old 02-26-2008, 11:46 PM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Nice. He said 'stopped' was lowercase though and he only wanted the last word, so maybe
Code:
 if (toupper($0) ~ pattern) {
   print $NF
 
Old 02-27-2008, 06:01 AM   #10
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks for the replies guys

I tried getting the exit status of the status.sh script, but unfortunately it always returned a successful "0".

ghostdog74/digiot:
That script works fine (using toupper()), and so far I have the following;

Code:
STATUS=""

# Get Server Status
get_server_status() 
{ 
 # grab the status of the server
 STATUS=`./serverStatus.sh $SERVER | awk 'END{
  pattern="STARTED|STARTING|STOPPED"

  if ( toupper($0) ~ pattern) {
    print toupper($NF)
  }
 }'`

 case $STATUS in
  STARTED)
   echo "Server \"$SERVER\" is Started";;
  STARTING)
   echo "Server \"$SERVER\" is Starting";;
  STOPPED.)
   echo "Server \"$SERVER\" is Stopped";;
  *)
   echo "Status Unknown for \"$SERVER\"";;
 esac
}

SERVER=Server1
get_server_status

SERVER=Server2
get_server_status
How does this look? Any suggestions for improvements? I'm not very familiar with bash coding etiquette so if there's a better way to do it I'm interested to hear

Cheers,
 
Old 02-27-2008, 07:17 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
good effort. When you are better at scripting as you go along, you can do everything (almost) in awk.
Just for the fun of it
Code:
./status.sh | awk -v server=$SERVER 'END{
  pattern="STARTED|STARTING|STOPPED"
  status["STARTED"] = server " is started."
  status["STOPPED"] = server " is stopped."
  status["STARTING"] = server " is starting."
  if ( toupper($0) ~ pattern) { 
     a=toupper($NF)
     sub(/\.$/,"",a)     
     print status[a]      
  }
 }'
keep up the good work
 
Old 02-27-2008, 07:44 AM   #12
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by piercey View Post
How does this look? Any suggestions for improvements? I'm not very familiar with bash coding etiquette so if there's a better way to do it I'm interested to hear

Cheers,
As far as less drastic revisions, I write functions as
Code:
get_server_status() {
    foo
}
rather than
Code:
get_server_status()
{
    foo
}
but I'm probably in the minority there and that's purely style and irrelevant to anything else.

Less trivially, unless there's a reason not to, I'd get in the habit of using $(foo) for command expansion rather than `foo`. I usually use `foo` on the command line because it's quicker but $(foo $(bar)) sort of stuff nests better. Lastly, functions can take arguments, so I'd probably delete the assignments to SERVER and say

get_server_status SERVER1
get_server_status SERVER2

and replace all $SERVER instances in the function with $1.

Actually, lastly lastly, you might want your functions and scripts returning exit codes. That's probably why serverStatus.sh returns 0 no matter what - it doesn't set an exit code and return >0 when it can't get a status. But, yeah, like ghostdog74 says, "keep up the good work".
 
Old 02-27-2008, 10:21 AM   #13
piercey
LQ Newbie
 
Registered: Sep 2007
Distribution: Gentoo, RHEL 5.2, CentOS 5
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks for the help guys, I really appreciate it
 
  


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
Can I use grep inside awk? Helene Programming 10 09-29-2015 08:48 PM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 12:37 PM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM

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

All times are GMT -5. The time now is 02:35 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