我正在制作一个脚本来从XML中提取字段,现在我得到了这个,我需要让它工作,我正在尝试2 for和greps,我需要一些帮助这个
#! /bin/bash
function charge_files () {
XML="Prueba.xml";
if [ -f "$XML" ]; then
echo "=============================";
echo "| XML CHARGED |";
echo "=============================";
else
echo "=============================";
echo "| XML NOT CHARGED |";
echo "=============================";
fi
}
function extract () {
#extract all from the file (not curr working)
x=`grep "Host"`
for $x in "$XML"
do
for LINEA in `cat $XML | grep "<Telegram" ` #LINEA guarda el resultado del fichero datos.txt
do
TIMESTAMP=`echo $LINEA | grep [Timestamp="*"] ` #Extracts TIMESTAMP
FRAMEFORMAT=`echo $LINEA | grep [FrameFormat="*"]` #Extracts FRAMEFORMAT
RAWDATA=`echo $LINEA | grep [RawData="*"]` #Extracts RAWDATA
echo "$x $HOST $TIMESTAMP $FRAMEFORMAT $RAWDATA" >> output.logs #Shows result
done
done
}
charge_files
extract
我用这个字段获得了这个xml
<CommunicationLog xmlns="http://knx.org/xml/telegrams/01">
<RecordStart Timestamp="" Mode="" Host="PC1" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<RecordStart Timestamp="" Mode="" Host="PC2" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<RecordStop Timestamp="" />
</CommunicationLog>
我想要这样的输出来进行更多的比较:
HOST="PC1" ConnectorType="" Timestamp="" FrameFormat="" RawData=""
HOST="PC1" ConnectorType="" Timestamp="" FrameFormat="" RawData=""
HOST="PC2" ConnectorType="" Timestamp="" FrameFormat="" RawData=""
HOST="PC2" ConnectorType="" Timestamp="" FrameFormat="" RawData=""
答案 0 :(得分:0)
代码存在许多问题。
提取物:
假设:
所以这是我用于测试的XML文件:
<CommunicationLog xmlns="http://knx.org/xml/telegrams/01">
<RecordStart Timestamp="" Mode="" Host="PC1" ConnectionName="name1" ConnectionOptions="option1" ConnectorType="type1" MediumType="med1" />
<Telegram Timestamp="t1a" Service="s1a" FrameFormat="ff1a" RawData="rd1a" />
<Telegram Timestamp="t1b" Service="s1b" FrameFormat="ff1b" RawData="rd1b" />
<RecordStart Timestamp="" Mode="" Host="PC2" ConnectionName="name2" ConnectionOptions="option2" ConnectorType="type2" MediumType="med2" />
<Telegram Timestamp="t2a" Service="s2a" FrameFormat="ff2a" RawData="rd2a" />
<Telegram Timestamp="t2b" Service="s2b" FrameFormat="ff2b" RawData="rd2b" />
<RecordStop Timestamp="stoptimestamp" />
</CommunicationLog>
这里是剧本:
#! /bin/bash
function charge_files ()
{
XML="Prueba.xml"
if [ -f "$XML" ]; then
echo "============================="
echo "| XML CHARGED |"
echo "============================="
else
echo "============================="
echo "| XML NOT CHARGED |"
echo "============================="
exit 1
fi
}
function extract ()
{
host=''
while IFS= read -r line; do
# Find if it is a RecordtStart line
if [ $(echo $line | grep -c "RecordStart") -eq 1 ]
then
# If host == '', then it is the first host we see.
# Otherwise, we are changing host, so print an empty line
if [ "$host" != '' ]
then
echo ""
fi
# Collect the host information
host=$(echo $line | awk '{print $4}' | cut -d'"' -f2)
# Collect the ConnectorType information
connectortype=$(echo $line | awk '{print $7}')
# Done with this loop in the while, move on to the next
continue
fi
# Find if it is a Telegram line
if [ $(echo $line | grep -c "Telegram") -eq 1 ]
then
# Collect the Timestamp information
timestamp=$(echo $line | awk '{print $2}')
# Collect the FrameFormat information
frameformat=$(echo $line | awk '{print $4}')
# Collect the RawData information
rawdata=$(echo $line | awk '{print $5}')
# Print the information
echo "HOST=\"$host\" $connectortype $timestamp $frameformat $rawdata"
# Done with this loop in the while, move on to the next
continue
fi
done <$XML
}
charge_files
extract
产生了这个输出:
=============================
| XML CHARGED |
=============================
HOST="PC1" ConnectorType="type1" Timestamp="t1a" FrameFormat="ff1a" RawData="rd1a"
HOST="PC1" ConnectorType="type1" Timestamp="t1b" FrameFormat="ff1b" RawData="rd1b"
HOST="PC2" ConnectorType="type2" Timestamp="t2a" FrameFormat="ff2a" RawData="rd2a"
HOST="PC2" ConnectorType="type2" Timestamp="t2b" FrameFormat="ff2b" RawData="rd2b"