Linux 中如何解析获取文件中XML标签对中的值
举例:文件内容:sdfs<abc>hehehe</abc>sdf,如何获取字符串“hehehe”;或者获取字符串“<abc>hehehe</abc>”(不展示首尾的其他字...
举例:文件内容:sdfs<abc>hehehe</abc>sdf,如何获取字符串“hehehe”;
或者获取字符串“<abc>hehehe</abc>”(不展示首尾的其他字符串)
试过用awk,awk -F: '/<abc>.*<\/abc>/' 文件名 ,但是只能得到所在行,不能得到指定字符串,请各位大拿帮忙,谢谢! 展开
或者获取字符串“<abc>hehehe</abc>”(不展示首尾的其他字符串)
试过用awk,awk -F: '/<abc>.*<\/abc>/' 文件名 ,但是只能得到所在行,不能得到指定字符串,请各位大拿帮忙,谢谢! 展开
推荐于2018-05-10 · 知道合伙人互联网行家
关注
展开全部
如果获取到节点话,就调用attributeValue(String name)方法获取里面的值就好了。
$cat test.sh
#!/bin/bash
if [ -z $1 ];then
echo 'USAGE:COMMAND FILENAME'
exit 0
fi
filename=record.txt
HOST=(`sed -n 's/.*>\(.*\)<\/host>/\1/p' $1`)
OIDG=(`sed -n 's/.*>\(.*\)<\/oidgroupname>/\1/p' $1`)
COMM=(`sed -n 's/.*>\(.*\)<\/communitystring>/\1/p' $1`)
DESC=(`sed -n 's/.*>\(.*\)<\/description>/\1/p' $1`)
FILE=(`ls -l $filename >/dev/null 2>&1 | awk '{print $8}'`)
if [ ! -z $FILE ];then
echo -e "host\t\toidgroupname\t\tcomm\t\tdesc" >$filename
fi
for((i=0;i<${#HOST[@]};i++));do
echo -e "${HOST[i]}\t${OIDG[i]}\t${COMM[i]}\t\t${DESC[i]}" >>$filename
done
$./test.sh file
$cat record.txt
host oidgroupname comm desc
192.168.1.1 CpuUtilization_MF public 192.168.1.1_CPUUtilizaton
192.168.1.2 CpuUtilization_MF public 192.168.1.2_CPUUtilizaton
192.168.1.3 CpuUtilization_MF public 192.168.1.3_CPUUtilizaton
192.168.1.4 CpuUtilization_MF public 192.168.1.4_CPUUtilizaton
192.168.1.5 CpuUtilization_MF public 192.168.1.5_CPUUtilizaton
$cat test.sh
#!/bin/bash
if [ -z $1 ];then
echo 'USAGE:COMMAND FILENAME'
exit 0
fi
filename=record.txt
HOST=(`sed -n 's/.*>\(.*\)<\/host>/\1/p' $1`)
OIDG=(`sed -n 's/.*>\(.*\)<\/oidgroupname>/\1/p' $1`)
COMM=(`sed -n 's/.*>\(.*\)<\/communitystring>/\1/p' $1`)
DESC=(`sed -n 's/.*>\(.*\)<\/description>/\1/p' $1`)
FILE=(`ls -l $filename >/dev/null 2>&1 | awk '{print $8}'`)
if [ ! -z $FILE ];then
echo -e "host\t\toidgroupname\t\tcomm\t\tdesc" >$filename
fi
for((i=0;i<${#HOST[@]};i++));do
echo -e "${HOST[i]}\t${OIDG[i]}\t${COMM[i]}\t\t${DESC[i]}" >>$filename
done
$./test.sh file
$cat record.txt
host oidgroupname comm desc
192.168.1.1 CpuUtilization_MF public 192.168.1.1_CPUUtilizaton
192.168.1.2 CpuUtilization_MF public 192.168.1.2_CPUUtilizaton
192.168.1.3 CpuUtilization_MF public 192.168.1.3_CPUUtilizaton
192.168.1.4 CpuUtilization_MF public 192.168.1.4_CPUUtilizaton
192.168.1.5 CpuUtilization_MF public 192.168.1.5_CPUUtilizaton
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询