Hi,
I have a file having rows as
Row1 : model=.1.3.6.1.4.1.9.1.1047,location=abc, pollgrp=PG_CISCO4, ifindex=3, ip=10.10.10.1,parttype=Interface, devtype=Router,part=GigabitEthernet0/1,ifmtu=1520
Row2 : model=.1.3.6.1.4.1.9.1.2047,pollgrp=PG_ABCD4,ifindex=3,contact=LMN,ip=20.20.20.1,devtype=Router,ifmtu=2345
Row3:model=.1.3.6.1.4.1.9.1.3047,location=xyz,devtype=route,dwelf=xxxx
I need output as below
model;location;pollgrp;ifindex;contact;ip;parttype;devtype;part;ifmtu;dwelf
.1.3.6.1.4.1.9.1.1047;abc;PG_CISCO4;3;;10.10.10.1;Interface;Router;GigabitEthernet0/1;1520;
.1.3.6.1.4.1.9.1.2047;;PG_ABCD4;3;LMN;20.20.20.1;;Router;;2345;
.1.3.6.1.4.1.9.1.3047;xyz;;;;;;route;;;xxxx
Can someone help me out?
RudiC
2
Any attempts from your side?
---------- Post updated at 15:17 ---------- Previous update was at 15:12 ----------
Howsoever, try
awk -F, '
BEGIN {HDStr = "model;location;pollgrp;ifindex;contact;ip;parttype;devtype;part;ifmtu;dwelf"
for (HDCNT=i=split(HDStr, HD, ";"); i>0; i--) COL[HD]
print HDStr
}
{gsub (/ /,"")
for (i=1; i<=NF; i++) {split ($i, T, "=")
RES[T[1]]=T[2]
}
DL=""
for (i=1; i<=HDCNT; i++) {printf "%s%s", DL, RES[HD]
DL=";"
}
printf "\n"
split ("", RES)
}
' file
model;location;pollgrp;ifindex;contact;ip;parttype;devtype;part;ifmtu;dwelf
.1.3.6.1.4.1.9.1.1047;abc;PG_CISCO4;3;;10.10.10.1;Interface;Router;GigabitEthernet0/1;1520;
.1.3.6.1.4.1.9.1.2047;;PG_ABCD4;3;LMN;20.20.20.1;;Router;;2345;
.1.3.6.1.4.1.9.1.3047;xyz;;;;;;route;;;xxxx
taking into account that your sample file's fields are not built consistently; obviously some have spaces and some don't.
The cut command should give you what you want.
Here is a sample script.
$ cat mydata.dat
model;location;pollgrp;ifindex;contact;ip;parttype;devtype;part;ifmtu;dwelf
.1.3.6.1.4.1.9.1.1047;abc;PG_CISCO4;3;;10.10.10.1;Interface;Router;GigabitEthernet0/1;1520;
.1.3.6.1.4.1.9.1.2047;;PG_ABCD4;3;LMN;20.20.20.1;;Router;;2345;
.1.3.6.1.4.1.9.1.3047;xyz;;;;;;route;;;xxxx
$ cat runme.sh
#!/bin/bash
MY_HEADER="model;location;pollgrp;ifindex;contact;ip;parttype;devtype;part;ifmtu;dwelf"
for line in `cat mydata.dat`
do
for i in {1..11}
do
THIS_FIELD=`echo $MY_HEADER | cut -d";" -f$i`
THIS_VALUE=`echo $line | cut -d";" -f$i`
printf "%15s" "${THIS_FIELD}: "
echo "|${THIS_VALUE}|"
done
echo " "
done
$ ./runme.sh
model: |model|
location: |location|
pollgrp: |pollgrp|
ifindex: |ifindex|
contact: |contact|
ip: |ip|
parttype: |parttype|
devtype: |devtype|
part: |part|
ifmtu: |ifmtu|
dwelf: |dwelf|
model: |.1.3.6.1.4.1.9.1.1047|
location: |abc|
pollgrp: |PG_CISCO4|
ifindex: |3|
contact: ||
ip: |10.10.10.1|
parttype: |Interface|
devtype: |Router|
part: |GigabitEthernet0/1|
ifmtu: |1520|
dwelf: ||
model: |.1.3.6.1.4.1.9.1.2047|
location: ||
pollgrp: |PG_ABCD4|
ifindex: |3|
contact: |LMN|
ip: |20.20.20.1|
parttype: ||
devtype: |Router|
part: ||
ifmtu: |2345|
dwelf: ||
model: |.1.3.6.1.4.1.9.1.3047|
location: |xyz|
pollgrp: ||
ifindex: ||
contact: ||
ip: ||
parttype: ||
devtype: |route|
part: ||
ifmtu: ||
dwelf: |xxxx|