用awk sed的解析更新文件的傀儡 [英] using awk sed to parse update puppet file
问题描述
我有一些code线的,有一个部分,看起来像这样的傀儡文件:
I have a puppet file with a number of lines of code that has a section that looks like this:
$defaultrepo=myrepo
$defaultbranch=mybranch
gitmod::pullstuff {'othergitcode':
gitcommit => "b54123be540adrwer3b65872384e0101c5f94c926b81",
gitorg => "${defaultrepo}",
branch => "${defaultbranch}",
}
gitmod::pullstuff {'mygitcode':
gitcommit => "b54123be540adrfer3b65872384e0101c5f94c926b81",
gitorg => 'awesomerepo',
branch => "master",
}
它可以有任意数量的块,将类似于上述code的两个街区。
It can have any number of blocks that will look similar to the two blocks of code listed above.
我要寻找一种方式来解析文件,并拉出值作为变量,然后用它们来获得介绍最新git的承诺并更新值。我在想的awk和bash脚本的sed,以便它可以做这样的事情。
例如
I am looking for a way to parse the file and pull out the values as variables then use them to obtain the lastest git commit and update the value. I was thinking of awk and sed in a bash script so that it can do something like this. e.g.
值可以是:
$ gitrepo = mygit code
$ gitorg = awesomerepo
$ gitbranch =主
for i in $(awk "regexto pull out each block from gitmod::pullstuff \{'whatever': through to the ending \}" puppetfile.pp);
do
$gitrepo="sed/awk to pullout git repo"
$gitorg="sed/awk to pullout git org"
$gitbranch="sed/awk to pullout git branch"
$newcommit=git ls-remote https://github.com/$gitorg/$gitrepo.git refs/heads/$gitbranch
sed -i "replace commit with $newcommit"
done
我不是一个开发商通过交易所以请多多包涵。可能有人请指出一般结构应该是什么样子。不要担心,正则表达式等我能勾搭,并找出答案。只是想知道我怎样才能得到它,这样它把每一个块它单独认定。出于某种原因,我不能让awk将它们正确地分离出来,它弄乱的格式。
I am not a developer by trade so please bear with me. Could someone please point out what the general structure should look like. Don't worry about the regex etc I can mess around and figure that out. Just wondering how I can get it so that it treats each "block" it finds individually. For some reason I can't get awk to separate them out properly and it messes up the formatting.
就像我可以做这样的事情:
Like I can do something like this:
awk的/ gitmod :: pullstuff / {a = 4} {如果(一个 - - 0){打印;接下来}}
,它会拉出我想要的一切。我需要的是某种方式将其放入for语句,这样我可以治疗了awk认定为一个单独的块,我可以做的东西太多每个实例。
and it will pull out everything I want. What I need is to somehow stick it into a for statement so that I can treat each instance that awk finds as a separate block that I can do stuff too.
推荐答案
您可以尝试这样的:
awk -f m.awk puppetfile.pp puppetfile.pp
其中, m.awk
是:
NR==FNR {
if (/^gitmod/) {
gitrepo=getRepo()
getline
getline
gitorg=getOrg()
getline
branch=getBranch()
com[++i]=getNewCommit()
}
else if (/^\$[[:alnum:]]*=/) {
vn=getVarName()
val=getVarValue()
var[vn]=val
}
next
}
/^gitmod/ {
print
getline
sub(/".*"/,"\""com[++j]"\"")
}
{ print }
function getVarValue(a) {
match($0,/=([[:alnum:]]+)[[:blank:]]*/,a)
return a[1]
}
function getVarName(a) {
match($0,/\$([[:alnum:]]+)=/,a)
return "${"a[1]"}"
}
function getNewCommit(cmd,var) {
cmd="ls-remote https://github.com/"gitorg"/"gitrepo".git refs/heads/"branch
cmd |& getline var
return var
}
function getBranch(a,br) {
match($0,/"(.*)"/,a)
br=a[1]
if (br in var) br=var[br]
return br
}
function getOrg(a,org) {
match($0,/"(.*)"/,a)
org=a[1]
if (org in var) org=var[org]
return org
}
function getRepo(a,rep) {
match($0,/\{"(.*)":/,a)
rep=a[1]
if (rep in var) rep=var[rep]
return rep
}
这篇关于用awk sed的解析更新文件的傀儡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!