为了给出一些上下文,我有一个构build服务器任务,需要更新一个configuration文件(yaml格式)字段值,并推到回购使用later.My立即想到使用某种forms的正则expression式search/replace,但我'想知道是否有其他更好的方法来完成这个?
即:
# Before my_field : 2 # After my_field : 3.1
当然,该文件将包含其他值需要保持不变的字段。
编辑
所以根据rbtux的build议,我find了一个ruby yaml模块来解决我的问题。 这是如何工作的:
source.yaml:
--- some_value: 1 my_value: 2 new_value: 3
replace.ruby:
require 'yaml' hiera = YAML.load_file('source.yaml'); hiera['my_value'] = "other data" File.open('source.yaml','r+') do |h| h.write hiera.to_yaml end puts hiera['my_value'] # outputs my_value as 'other data' with other variables intact
你可以扩展它来传递值作为参数。
最好的办法是select一个脚本语言perl,python,ruby等提供yaml操作实用程序。
如果该文件足够简单,你可以逃脱sed黑客:
sed 's/^\(\s*my_field\s*:\s*\).*/\1new-value/'