我有以下变量:
tags = {
environment = "development",
provider = "ServiceOne",
ansible_role = "nfs-role",
comment = "mysql"
}
在我的管道中,我需要将其转换为以下内容:
tfh pushvars -overwrite-all -dry-run false -hcl-var "tags={environment="development", provider="ServiceOne", ansible_role="nfs-rolep",comment= "mysql"}"
我试过了SEDAWK和但是没有任何结果?
这是我现在站的地方:
#!/bin/bash
#[[ -z "$2" ]] && echo "==> Usage: ./transform_tfe_vars.sh <<INPUT_FILE>> <<OUTPUT_FILE>>" && exit 1;
vars_file=${1}
#output_file=${2}
tmp_file=".todelete.tmp"
cmd "$vars_file" | grep -v '^#' | awk '!/^$/' > "$tmp_file"
while read -r p; do
a=$(echo "$p" | awk '{print $1}')
b=$(echo "$p" | awk '{print $3}')
echo "tfh pushvars -overwrite-all -dry-run false -shcl-var "$a=\""$b""""
done <$tmp_file
shell读循环总是处理文本的错误方法,请参阅为什么使用shell-loop来处理文本被认为是不好的做法。发明shell的人还发明了awk,让shell调用它来操作文本。
看起来这可能是你想要做的:
#!/usr/bin/env bash
(( $# == 2 )) || { echo "==> Usage: ${0##*/} <<INPUT_FILE>> <<OUTPUT_FILE>>"; exit 1; }
vars_file="$1"
output_file="$2"
awk '
BEGIN {
ORS = ""
print "tfh pushvars -overwrite-all -dry-run false -hcl-var ""
}
NF && !/^#/ {
gsub(/[[:space:]]/,"")
gsub(/"/,"\\&")
print
}
END {
print ""n"
}
' "$vars_file" > "$output_file"