Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We have an application that grabs it's setup parameters from a file. I want to extract one or two statements from the startup string and present them as a nice table.

An example string would be -Dmysql.host=mysql1.company.com but it might also be an ipaddress or a machine name and not an fqdn.

I want locate the -Dmysql.host= but return the servername.

Any tips or pointers as to how to, once I've found the string "-Dmysql.host=" in the file, show everything to the next white space would be appreciated.

Perhaps there is a better method. I plan on running this on a dozen machines or so eventually to return a list of which application machines are configured to talk to which db machine at a glance.

Thanks you for your time.

share|improve this question
    
It would seem that sed is a popular answer. :) –  crb Jun 17 '09 at 14:46
add comment

3 Answers

up vote 4 down vote accepted

Try this:

grep mysql.host file.txt | sed -e 's/.*mysql.host=\(\S*\).*/\1/g'

You should end up with a value that is the value of mysql.host. You can put the -D in the match also if you want, but I have left it out because it is a parameter to grep and you need to escape it.

share|improve this answer
2  
You might want to escape the dot in mysql.host as dot is a regex atom in grep so would match "mysqlshost" or "mysql-host" as well as "mysql.host" –  Jason Tan Jun 18 '09 at 6:46
    
+1 good catch, although the assumption here was there would only be one occurrence in the parameters file. If not, you might want a '| head -n 1' in there also! –  crb Jun 18 '09 at 12:25
add comment

How about feeding the files to good old sed:

$ echo foo  -Dmysql.host=mysql1.company.com -bar | grep -- "-Dmysql.host=" | sed -e 's/^.*-Dmysql.host=//' -e 's/ .*//'
mysql1.company.com

So in your case you'd feed it the file like so:

$ grep -- "-Dmysql.host=" FILENAME | sed -e 's/^.*-Dmysql.host=//' -e 's/ .*//'

I realize there's a grep in there that sed could handle but this is just easier to write, sed can be awkward.

share|improve this answer
add comment

From the information given, I would go about this by first splitting everything based on whitespace, and then split those tokens based on the = sign. Pure bash solution would be something like what follows.

  foo="bar=baz boo=zip"
  for keyvalue in $foo; do
     key=${keyvalue%%=*} 
     value=${keyvalue##*=}
     echo $key $value
  done

Since IFS is probably whitespace, the shell takes care of splitting based on the whitespace. You can then use Parameter (Variable) Expansion to handle spiting into key / value pairs based on the equals sign.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.