I have two scripts, each script reads an individual data file and copies specific lines of data and sends to MySQL table. Only difference is, each script sends data to a separate column on the same DB. I want to use one script to populate DB table and have data look horizontal, with no overlapping. So far my database looks like attached image, named sendtodb.png.
I decided to combine two scripts into one (master) script. When I run master script, it only populates data to one column on the DB. I want to send data to two specific columns.
I know exact problem, my script can't distinguish between what data I want to send to what column. I'm using one CSV file that has all the data I need. When I run master script, it reads from CSV file and copies specific lines I need, and sends to DB.
I've been playing around with awk command in hopes it can filter out lines of data I don't need, but so far my grep command copies exact lines I want to send to the DB columns.
My script is below.
#!/bin/bash
a="grep -H -R -n "STRING" /usr/local/bin/Production/me.csv"
a2="grep -H -R -n "OID:" /usr/local/bin/Production/me.csv"
# Condition will return true only if there is a file in directory
# Condition Not true, script will do nothing and script ends
if [ `$a | grep -c "STRING"` -gt 0 ]
then
# Going to copy entire contents after STRING:
grep -oP 'STRING:\K.*' me.csv > fixme.csv
# Command to remove "\\" from SNMP output
# Sed command only works when running hrstoragedesc script
sed 's/\\//g' /usr/local/bin/Production/fixme.csv > sendtodb.csv
fi
# Current date will display
fetcha=$(date +"%Y-%m-%d")
if [ `$a2 | grep -c "OID:"` -gt 0 ]
then
grep -oP 'OID:\K.*' me.csv >> sendtodb.csv
fi
IFS=,
while read hrstoragedescr hrstoragetype
*** do
**** echo "INSERT INTO info (server,hrstoragedescr,hrstoragetype,date) VALUES ('EXCHANGE','$hrstoragedescr','$hrstoragetype','$fetcha');"
*
done < /usr/local/bin/Production/sendtodb.csv | mysql -u root -p SNMP;
My goal is to successfully send data to MySQL table and have the data look horizontal row by row. I also attached CSV file, where script reads from and copies data to sent to the DB.