3 files:
n.txt
id-3a,oc-ctrl-jr-0,ACTIVE,-,Running,cp=172.31.0.7
id-5e,oc-ctrl-jr-1,ACTIVE,-,Running,cp=172.31.0.6
id-5f,oc-ctrl-jr-2,ACTIVE,-,Running,cp=172.31.0.5
id-0,oc-comp-jr-0,ACTIVE,-,Running,cp=172.31.0.9
id-77,oc-comp-jr-1,ACTIVE,-,Running,cp=172.31.0.8
bm.txt
server-10,id-77,power on,active,False
server-2,id-5f,power on,active,False
server-32,id-3a,power on,active,False
server-11,id-5e,power on,active,False
server-25,id-0,power on,active,False
The 3rd file consists of sections for each row from bm.txt:
hosts.yaml
[..]
- arch: x86_64
zone: foo
cpu: 1
disk: 10
hw_model_type:
- bar-8
mac:
- aa:aa:aa:aa:aa:aa:aa
memory: 4096
name: server-32
desc: 'my host'
ip_addr: 192.168.117.33
info: false
type: bla
[..]
Required output:
n name,b name,n power,n desc,n state,n cp,bm power,bm state,b error,ip
oc-ctrl-jr-0,server-32,ACTIVE,-,Running,cp=172.31.0.7,power on,active,False,192.168.117.33
oc-ctrl-jr-1,server-11,ACTIVE,-,Running,cp=172.31.0.6,power on,active,False,192.168.117.47
oc-ctrl-jr-2,server-2,ACTIVE,-,Running,cp=172.31.0.5,power on,active,False,192.168.117.87
oc-comp-jr-0,server-25,ACTIVE,-,Running,cp=172.31.0.9,power on,active,False,192.168.117.111
oc-comp-jr-1,server-10,ACTIVE,-,Running,cp=172.31.0.8,power on,active,False,192.168.117.3
I'm able to join the first two files with this code, but the order of the columns is different from desired:
awk -F, 'BEGIN{print"N Name,N Power,N Desc,N State,N CP,BM Name,BM Power,BM State,BM Error"}
NR==FNR{OFS=",";a[$2]=$1 OFS $3 OFS $4 OFS $5; next}
$1 in a {print $2,$3,$4,$5,$6,a[$1]}' bm.txt n.txt
But I don't know how to reorder nor how to add the 3rd file parsing into the main code. I'm able to parse the 3rd file separately like so:
awk '$0~"name: server-32$"{getline;getline;print $NF}' hosts.yaml
I'll appreciate on how to get the required output. And any advice on how to improve the current code.
Thanks