awk - format output

Input file1

 
zone:  BAU_SERVER1 
                C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
 zone:  BAU_SERVER2  
                C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
 zone:  BAU_SERVER3    
                C0:50:76:01:C6:20:00:06; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
Effective configuration:
 cfg:   Fabric_A        
 zone:  BAU_SERVER1    
                c0:50:76:01:c6:20:00:12
                50:06:01:69:3b:20:14:8b
                50:06:01:60:3b:20:14:8b
                50:00:09:72:08:2D:95:10
 zone:  BAU_SERVER2    
                c0:50:76:01:c6:20:00:08
                50:06:01:69:3b:20:14:8b
                50:06:01:60:3b:20:14:8b
 zone:  BAU_SERVER3   
                c0:50:76:01:c6:20:00:06
                50:06:01:69:3b:20:14:8b

I have another ALIAS file ( for example called ALIAS1)..with this syntax

 
50:06:01:60:3b:20:14:8b,host1
50:06:01:69:3b:20:14:8b,host2
50:00:09:72:08:17:8d:40,host3
50:00:09:72:08:2D:95:10,host4
50:06:01:69:3b:20:10:67,host5
...

Required Output ( basically want to read the input below Effective configuration: )

 
Fabric_A,BAU_SERVER1,c0:50:76:01:c6:20:00:12,host1,host2,host4
Fabric_A,BAU_SERVER2,c0:50:76:01:c6:20:00:08,host1,host2
Fabric_A,BAU_SERVER3,c0:50:76:01:c6:20:00:06,host1

If there is no alias in the ALIAS file ,print the orignal number

Thanks
Mickey

 awk '
ARGIND==2{a[$1]=$2;next}
/cfg:/{p=$2;print ""}
/zone:/{printf "\n"p"," $2","}
/^  /{printf a[$1]?a[$1]",":$1","}
END{print ""}
' FS=","  alias FS=" " input

I get this as output

 
,BAU_SERVER1,C0:50:76:01:C6:20:00:12;,50:06:01:60:3B:20:14:8B,
,BAU_SERVER2,C0:50:76:01:C6:20:00:08;,50:06:01:60:3B:20:14:8B,
,BAU_SERVER3,C0:50:76:01:C6:20:00:06;,50:06:01:60:3B:20:14:8B,
Fabric_A,BAU_SERVER1,c0:50:76:01:c6:20:00:12,50:06:01:69:3b:20:14:8b,50:06:01:60:3b:20:14:8b,50:00:09:72:08:2D:95:10,
Fabric_A,BAU_SERVER2,c0:50:76:01:c6:20:00:08,50:06:01:69:3b:20:14:8b,50:06:01:60:3b:20:14:8b,
Fabric_A,BAU_SERVER3,c0:50:76:01:c6:20:00:06,50:06:01:69:3b:20:14:8b,

Thanks for the quick reply

2 issues .... looks like it does not use the ALIAS file .... also i want the data below the line "Effective configuration:" ... looks like its getting the data above that too

---------- Post updated at 02:31 PM ---------- Previous update was at 02:22 PM ----------

My bad ... i left some part of the input file ... its actually like this

 
cfg:   CNJ_DCX_Fabric_A        
                BAU_SERVER1; BAU_SERVER2; BAU_SERVER3; 
                BAU_SERVER4; BAU_SERVER5; BAU_SERVER6;
 zone:  BAU_SERVER1 
                C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
 zone:  BAU_SERVER2  
                C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
 zone:  BAU_SERVER3    
                C0:50:76:01:C6:20:00:06; 50:06:01:69:3B:20:14:8B; 
                50:06:01:60:3B:20:14:8B
Effective configuration:
 cfg:   Fabric_A        
 zone:  BAU_SERVER1    
                c0:50:76:01:c6:20:00:12
                50:06:01:69:3b:20:14:8b
                50:06:01:60:3b:20:14:8b
                50:00:09:72:08:2D:95:10
 zone:  BAU_SERVER2    
                c0:50:76:01:c6:20:00:08
                50:06:01:69:3b:20:14:8b
                50:06:01:60:3b:20:14:8b
 zone:  BAU_SERVER3   
                c0:50:76:01:c6:20:00:06
                50:06:01:69:3b:20:14:8b

i want all the data .. below the "effective configuration" line

Thanks again

 awk 'ARGIND==2 {a[$1]=$2;next}
/Effective configuration:/{x=1}
x==1&&/cfg:/{p=$2;print ""}
x==1&&/zone:/{printf "\n"p"," $2","}
x==1&&/^  /{printf a[$1]?a[$1]",":$1","}
END{print ""}' FS=","  alias FS=" " input
 
Fabric_A,BAU_SERVER1,c0:50:76:01:c6:20:00:12,host2,host1,host4,
Fabric_A,BAU_SERVER2,c0:50:76:01:c6:20:00:08,host2,host1,
Fabric_A,BAU_SERVER3,c0:50:76:01:c6:20:00:06,host2,

just be aware that the 'ARGIND' is a GNU-specific extension not available in other awk-s.

A better 'paradigm' is:

FNR==NR  {a[$1]=$2;next}
1 Like