我有一个配置文件,需要使用 bash 解析出一些值
例如。里面的config.txt
some_var= Not_needed
tests= spec1.rb spec2.rb spec3.rb
some_other_var= Also_not_needed
基本上,我只需要获取“spec1.rb spec2.rb spec3.rb”,而无需从该行中删除所有其他行和“tests=”。
我有这个并且它可以工作,但我希望有一个更简单的方法来做到这一点。
while read run_line; do
if [[ $run_line =~ ^tests=* ]]; then
echo "FOUND"
all_selected_specs=`echo ${run_line} | sed 's/^tests= /''/'`
fi
done <${config_file}
echo "${all_selected_specs}"
最佳答案
all_selected_specs=$(awk -F '= ' '$1=="tests" {print $2}' "$config_file")
使用 "= "
字段分隔符,查找第一个字段为 tests
的行并打印第二个字段。
关于bash - 提取与模式匹配的部分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900803/