我有一个4列的.csv文件。我无法将第一列输入表中。以下是脚本:
# !/bin/bash
_csvfile="/logstash/Mysql-Data.csv"
# Create the tables if they dont exists
mysql -u user -ppassword logstash << eof
CREATE TABLE IF NOT EXISTS \`elkDevIndexAssoc\` (
cenDevSID varchar(255),
cenDevFQDN varchar(255),
cenDevIP varchar(255),
cenDevServiceName varchar(255)
)
eof
# Format the csv, replace ";" with ","
sed -i -e 's/;/,/g' $_csvfile
# sed -i -e 's/^/,/g' $_csvfile
IFS=,
while read column1 column2 column3 column4
do
echo "INSERT INTO elkDevIndexAssoc (cenDevSID,cenDevFQDN,cenDevIP,cenDevServiceName) VALUES ('$column1', '$column2', '$column3', '$column4');"
done < Mysql-Data.csv | mysql --user='user' --password='password' -D logstash;
如果我回显每个列变量(例如$column1-4),输出看起来很好。这样地:
123213 serverfqdn 127.0.0.1 mysql
但当我尝试将其导入mysql时,第一列丢失了,如下所示:
mysql> select * from elkDevIndexAssoc;
+-----------+------------------------------------------------+---------------- -+--------------------+
| column1 | column2 | column3 | column4 |
+-----------+------------------------------------------------+---------------- -+--------------------+
|column1 | column2 | column3 | column4
| | serverfqdn | 127.0.0.1 | mysql
我不明白当我将第一列导入mysql时,它是如何丢失的,但是当我单独回显每个变量时,它又是如何丢失的?
编辑:我的.csv文件的格式如下:
S123123,serverfqdn,127.0.0.1,service
S123123,serverfqdn,127.0.0.1,service
S123123,serverfqdn,127.0.0.1,service
S123123,serverfqdn,127.0.0.1,service
2条答案
按热度按时间smdnsysy1#
mysql命令应该在循环中:
ot:在echo字符串中终止mysql命令不需要分号。
oyjwcjzk2#
好的,这样就可以了(本地测试):
INSERT INTO elkDevIndexAssoc (cenDevSID,cenDevFQDN,cenDevIP,cenDevServiceName) VALUES ('S123123', 'serverfqdn', '127.0.0.1', 'service')
INSERT INTO elkDevIndexAssoc (cenDevSID,cenDevFQDN,cenDevIP,cenDevServiceName) VALUES ('S123123', 'serverfqdn', '127.0.0.1', 'service')
INSERT INTO elkDevIndexAssoc (cenDevSID,cenDevFQDN,cenDevIP,cenDevServiceName) VALUES ('S123123', 'serverfqdn', '127.0.0.1', 'service')
INSERT INTO elkDevIndexAssoc (cenDevSID,cenDevFQDN,cenDevIP,cenDevServiceName) VALUES ('S123123', 'serverfqdn', '127.0.0.1', 'service')
while ...
echo "..." | mysql -u... -p...
done ...