Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 but it might also be an ipaddress or a machine name and not an fqdn.

I want locate the but return the servername.

Any tips or pointers as to how to, once I've found the string "" 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
up vote 4 down vote accepted

Try this:

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

You should end up with a value that is the value of 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
You might want to escape the dot in as dot is a regex atom in grep so would match "mysqlshost" or "mysql-host" as well as "" – 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

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
     echo $key $value

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

How about feeding the files to good old sed:

$ echo foo -bar | grep -- "" | sed -e 's/^.*' -e 's/ .*//'

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

$ grep -- "" FILENAME | sed -e 's/^.*' -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

Your Answer


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.