A,32,0,1,通信站,8楼交换机房.东门门磁
A,32,1,1,通信站,8楼交换机房.西门门磁
A,32,2,0,通信站,8楼交换机房.交流屏故障
A,32,3,0,通信站,8楼交换机房.UPS运行状态
A,32,4,0,通信站,8楼交换机房.高湿度告警
2.如果不需要考虑复杂的情况,可以按照你的需要修改一下的bash脚本,将以上的CSV文件(我这将其改为TXT文件了)
转成需要格式的XML文件(fep_yx.txt -> 安 $1/最后字段首个.之前的名称/$2 的文件路径下 $1_$2.xml ):
e.g. 上面一段 将其分别转成 A/8楼交换机房/A_32.xml
#!/usr/bin/bash
export LC_ALL=zh_CN.GBK
bar="-------------------------------------------------------------"
#得到首列不重复的枚举
feps="$(cat fep_yx.txt | awk 'BEGIN{FS=","}{print $1}' |uniq | grep '^[A-Z]')"
echo "feps includes : "$feps
echo $bar
#对每个枚举,建立文件夹处理
for fep in $feps; do
echo $bar
echo "FEP:"$fep
[ -d $fep ] || mkdir -p $fep
touch $fep/fep
rm -Rf ./$fep/*
#对总文件过滤出首列相同的放到其文件夹下
cat fep_yx.txt | grep "^$fep" > ./$fep/$fep"_yx".txt
cat fep_yc.txt | grep "^$fep" > ./$fep/$fep"_yc".txt
#找出其最后字段首个.之前的名称
devs=$(cat ./"$fep"/$fep"_yx".txt | awk 'BEGIN{FS=","}{print $6}' | awk 'BEGIN{FS="."}{print $1}' |uniq )
for dev in $devs;do
#echo $dev
echo $dev >> $fep/_dev.txt
done
devs=$(cat ./"$fep"/"$fep"_yc.txt | awk 'BEGIN{FS=","}{print $6}' | awk 'BEGIN{FS="."}{print $1}' |uniq )
for dev in $devs;do
echo $dev >> $fep/_dev.txt
done
devs=$(cat $fep/_dev.txt | uniq)
for dev in $devs;do
[ $dev == "0" ]&& break;
echo "---------------DEV:-"$dev
[ -d $fep/$dev ] || mkdir -p $fep/$dev
cat ./"$fep"/"$fep"_yx.txt | grep "$dev" | sed "s/$dev\.//g" > ./$fep/$dev/"$fep"_yx.txt
cat ./"$fep"/"$fep"_yc.txt | grep "$dev" | sed "s/$dev\.//g" > ./$fep/$dev/"$fep"_yc.txt
#找出以上层次下的$2不同枚举
stas=$(cat $fep/$dev/"$fep"_yx.txt | awk 'BEGIN{FS=","}{print $2}' |uniq | grep '^[0-9]*') #<< $(cat $fep/$dev/fep_yc.txt | awk 'BEGIN{FS=","}{print $2}' |uniq | grep '^[0-9]*') | uniq)
echo "STA:--"$stas
for sta in $stas;do
echo "NOD:---""$fep ,$dev, $sta"
#开始转成xml
echo "<?xml version=\"1.0\" encoding=\"gb2312\" standalone=\"no\"?>" > ./$fep/$dev/"$fep"_"$sta".xml
echo "<点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
cat $fep/$dev/"$fep"_yx.txt | grep "$sta" > ./"$fep"/$dev/"$fep"_"$sta"_yx.txt
echo "<开关量点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
cat ./"$fep"/$dev/"$fep"_"$sta"_yx.txt | awk 'BEGIN{FS=","}{print "<ROW><NAME>"$6"</NAME><ALARM_VALUE>"$4"</ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>"$3"</NODE></ROW>"}' >> ./$fep/$dev/"$fep"_"$sta".xml
echo "</开关量点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
rm $fep/$dev/"$fep"_"$sta"_yx.txt
cat $fep/$dev/"$fep"_yc.txt | grep "$sta", > ./"$fep"/$dev/"$fep"_"$sta"_yc.txt
echo "<模拟量点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
cat ./"$fep"/$dev/"$fep"_"$sta"_yc.txt | awk 'BEGIN{FS=","}{print "<ROW><NAME>"$16"</NAME><UP_BOUND>"$7"</UP_BOUND><UP_DEGREE>"$9"</UP_DEGREE><LOW_BOUND>"$8"</LOW_BOUND><LOW_DEGREE>"$10"</LOW_DEGREE><UP_UP_BOUND>"$11"</UP_UP_BOUND><UP_UP_DEGREE>"$3"</UP_UP_DEGREE><LOW_LOW_BOUND>"$12"</LOW_LOW_BOUND><LOW_LOW_DEGREE>"$14"</LOW_LOW_DEGREE><NODE>"$3"</NODE><OFFSET>"$6"</OFFSET><CALC_METHOD>"$4"</CALC_METHOD><COEFF>"$5"</COEFF></ROW>"}' >> ./$fep/$dev/"$fep"_"$sta".xml
echo "</模拟量点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
echo "</点位信息>" >> ./$fep/$dev/"$fep"_"$sta".xml
rm $fep/$dev/"$fep"_"$sta"_yc.txt
# 清理中间文件
rm -Rf ./$fep/$dev/"$fep"_yx.txt ./$fep/$dev/"$fep"_yc.txt
done
rm -f $fep/_dev.txt $fep/"$fep"_yx.txt $fep/"$fep"_yc.txt
done
|
3.转换后的结果
<?xml version="1.0" encoding="gb2312" standalone="no"?>
<点位信息>
<开关量点位信息>
< ROW><NAME>东门门磁</NAME><ALARM_VALUE>1< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>0</NODE></ROW>
< ROW><NAME>西门门磁</NAME><ALARM_VALUE>1< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>1</NODE></ROW>
< ROW><NAME>交流屏故障</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>2</NODE></ROW>
< ROW><NAME>UPS运行状态</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>3</NODE></ROW>
< ROW><NAME>高湿度告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>4</NODE></ROW>
< ROW><NAME>低湿度告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>5</NODE></ROW>
< ROW><NAME>高温度告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>6</NODE></ROW>
< ROW><NAME>低温度告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>7</NODE></ROW>
< ROW><NAME>配线架AA列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>16</NODE></ROW>
< ROW><NAME>配线架A0列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>17</NODE></ROW>
< ROW><NAME>配线架A1列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>18</NODE></ROW>
< ROW><NAME>配线架A2列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>19</NODE></ROW>
< ROW><NAME>配线架A3列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>20</NODE></ROW>
< ROW><NAME>配线架A4列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>21</NODE></ROW>
< ROW><NAME>配线架A5列告警</NAME><ALARM_VALUE>0< /ALARM_VALUE><ALARM_DEGREE>162</ALARM_DEGREE><NODE>22</NODE></ROW>
</开关量点位信息>
<模拟量点位信息>
< ROW><NAME>温度</NAME><UP_BOUND>35.000< /UP_BOUND><UP_DEGREE>163</UP_DEGREE><LOW_BOUND>5.000</LOW_BOUND><LOW_DEGREE>163</LOW_DEGREE><UP_UP_BOUND>40.000</UP_UP_BOUND><UP_UP_DEGREE>0</UP_UP_DEGREE><LOW_LOW_BOUND>-2.000</LOW_LOW_BOUND><LOW_LOW_DEGREE>162</LOW_LOW_DEGREE><NODE>0</NODE><OFFSET>0.000</OFFSET><CALC_METHOD>0</CALC_METHOD><COEFF>-1.2207e-002</COEFF></ROW>
< ROW><NAME>湿度</NAME><UP_BOUND>95.000< /UP_BOUND><UP_DEGREE>164</UP_DEGREE><LOW_BOUND>30.000</LOW_BOUND><LOW_DEGREE>164</LOW_DEGREE><UP_UP_BOUND>100.000</UP_UP_BOUND><UP_UP_DEGREE>1</UP_UP_DEGREE><LOW_LOW_BOUND>10.000</LOW_LOW_BOUND><LOW_LOW_DEGREE>162</LOW_LOW_DEGREE><NODE>1</NODE><OFFSET>0.000</OFFSET><CALC_METHOD>0</CALC_METHOD><COEFF>-2.4414e-002</COEFF></ROW>
</模拟量点位信息>
</点位信息>
|