我有一个执行以下任务的shell脚本:
ec2-describe-instances
命令中提取标记(name,owner,cost.centre)并将此详细信息写入文件。赞赏任何领导
答案 0 :(得分:1)
忘了你的,我建议在awk中使用2-d数组来简化代码。
以下是您上一个问题(Error in shell script and how to write to a file)
的更新awk 'BEGIN{print "Instance id Name Owner Cost.centre"}
/TAG/{split($0,a,FS);a[4]=tolower(a[4]);$1=$2=$3=$4="";b[a[3],a[4]]=$0;c[a[3]]}
END{for (i in c) printf "%-18s%-26s%-14s%-20s\n",i,b[i,"name"]==""?"Unknown":b[i,"name"],b[i,"owner"]==""?"Unknown":b[i,"owner"],b[i,"cost.center"]==""?"Unknown":b[i,"cost.center"]}' file