使用bash从属性文件中提取值 [英] Extract values from a property file using bash

查看:53
本文介绍了使用bash从属性文件中提取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量,其中包含用空格分隔的键/值:

I have a variable which contains key/values separated by space:

echo $PROPERTY 

server_geo=BOS db.jdbc_url=jdbc\:mysql\://mysql-test.com\:3306/db02 db.name=db02 db.hostname=/mysql-test.com datasource.class.xa=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource server_uid=BOS_mysql57 hibernate33.dialect=org.hibernate.dialect.MySQL5InnoDBDialect hibernate.connection.username=db02 server_labels=mysql57,mysql5,mysql db.jdbc_class=com.mysql.jdbc.Driver db.schema=db02 hibernate.connection.driver_class=com.mysql.jdbc.Driver uuid=a19ua19 db.primary_label=mysql57 db.port=3306 server_label_primary=mysql57 hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect 

我需要提取单个键的值,例如 db.jdbc_url .使用一个代码段,我发现:

I'd need to extract the values of the single keys, for example db.jdbc_url. Using one code snippet I've found:

echo $PROPERTY | sed -e 's/ db.jdbc_url=\(\S*\).*/\1/g'

但这还会返回在我的密钥之前找到的其他属性.任何帮助如何解决它?谢谢

but that returns also other properties found before my key. Any help how to fix it ? Thanks

推荐答案

这是由于您使用替代命令( sed s/.../.../)引起的,因此正则表达式之前的文本保持不变.在 db \ .jdbc_url 之前使用.* 以及字符串的开始( ^ )/结束( $ )标记使您可以匹配变量的全部内容.

This is caused because you are using the substitute command (sed s/.../.../), so any text before your regex is kept as is. Using .* before db\.jdbc_url along with the begin (^) / end ($) of string marks makes you match the whole content of the variable.

为了完全安全,您的正则表达式应为:

In order to be totaly safe, your regex should be :

sed -e's/^.* db \ .jdbc_url = \(\ S * \).* $/\ 1/g'

这篇关于使用bash从属性文件中提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆