I have a script and I need to optimize it to reduce the execution time of this, currently, 6000 lines takes 1 minute and 38 seconds, I would like to reduce the time to take much less and I do not know how to optimize it anymore, I paste the script:
#! /bin/bash
function charge_files ()
{
database="test.db"
file="Prueba"
file2="def"
schema="esquema" #schema sqlite3
XML="Prueba.xml" #telegramas.xml
tempschema="tempschema"
if [ -f $XML ]; then
echo "============================="
echo "| XML CHARGED |"
echo "============================="
else
echo "============================="
echo "| XML NOT CHARGED |"
echo "============================="
exit 1
fi
}
function extract ()
{
host=''
i=0
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
connectioname=$(echo "$line" | awk '{print $5}' | cut -d'=' -f2)
# Collect the ConnectorType information
connectortype=$(echo "$line" | awk '{print $7}' | cut -d";" -f2 | cut -d"=" -f2)
# 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}' | cut -d"." -f1 | cut -d"=" -f2)
# Collect the service information
service=$(echo "$line" | awk '{print $3}' | cut -d"=" -f2)
# Collect the FrameFormat information
frameformat=$(echo "$line" | awk '{print $4}' | cut -d"=" -f2)
# Collect the RawData information
RawDatahost=$(echo "$line" | awk '{print $5}' | cut -c 36-39)
#Collect the RawDate information2
RawDatahost3=$(echo "$line" | awk '{print $5}' | cut -c 50-53)
# Print the information
i=$((i + 1))
echo "$connectioname $connectortype $timestamp $service $frameformat $((16#$RawDatahost)) $((16#$RawDatahost3))" >> $file
# Done with this loop in the while, move on to the next
continue
fi
done <$XML
}
function clean() {
#Clean the file
cat $file | tr -d '"' | tr -s " ">> $file2
cat $file2 | tr ' ' ',' >> definitivo
}
function create_schema(){
if [ -f "$schema" ]; then
echo "============================="
echo "| LOADED SCHEMA |"
echo "============================="
else
echo 'CREATE TABLE test (
KKID INTEGER PRIMARY KEY,
conection VARCHAR(20) NOT NULL,
ip VARCHAR(20) NOT NULL,
time DATETIME NOT NULL DEFAULT (strftime("%Y-%m-%d %H:%M:%S")),
service VARCHAR(20) NOT NULL,
frameformat VARCHAR(20) NOT NULL,
id_dispositivo VARCHAR(20) NOT NULL,
id_valor VARCHAR(20) NOT NULL
);' >> $schema
fi
if [ -f "$tempschema" ]; then
echo "============================="
echo "| LOADED TEMPSCHEMA |"
echo "============================="
else
echo 'create table temp (
conection VARCHAR(20) NOT NULL,
ip VARCHAR(20) NOT NULL,
time DATETIME NOT NULL DEFAULT (strftime("%Y-%m-%d %H:%M:%S")),
service VARCHAR(20) NOT NULL,
frameformat VARCHAR(20) NOT NULL,
id_dispositivo VARCHAR(20) NOT NULL,
id_valor VARCHAR(20) NOT NULL
);
.separator ","
.import ./definitivo temp
.exit' >> $tempschema
fi
}
function upload() {
#upload the schema to sqlite3 database
echo "$(sqlite3 "$database" < "$schema")"
#Create a temp table with the script
echo "$(sqlite3 $database < $tempschema)"
#upload the csv to a temp table
echo -e ".separator ","\\n.import definitivo temp" | sqlite3 $database
#make an insert from the temp to the database to get the atribute autoincrement
echo "$(sqlite3 $database "insert into test (conection, ip, time, service, frameformat, id_dispositivo, id_valor)SELECT * FROM temp;")"
#delate de table temp
echo "$(sqlite3 $database "drop table if exists temp;")"
#remove duplicated fields
echo "$(sqlite3 $database "DELETE FROM test WHERE oid NOT IN (SELECT min(oid) FROM test GROUP BY conection, ip, time, service, frameformat, id_dispositivo, id_valor)")"
rm definitivo
rm "$file"
rm "$file2"
}
charge_files
extract
clean
create_schema
upload
For those who know the script, pass an xml to a very simple csv format, remove the quotes and leave it clean so that later it can be introduced to a sqlite database, the schema has it in the same code, so that it is even simpler and create the necessary files for its execution.
I now attach the xml format, to think that xml can reach more than 7000 lines, but I'll give you an example
<CommunicationLog xmlns="http:telegrams">
<RecordStart Timestamp="" Mode="" Host="" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<RecordStop Timestamp="" />
<RecordStart Timestamp="" Mode="" Host="" ConnectionName="" ConnectionOptions="" ConnectorType="" MediumType="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<Telegram Timestamp="" Service="" FrameFormat="" RawData="" />
<RecordStop Timestamp="" />
</CommunicationLog>