Bash – Shell script to extract string from a file and return the next chars until a whitespace

bashscriptingshell

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.

Best Answer

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.