如何使用while循环从bash将文本文件导入mysql表

时间:2019-03-21 16:30:35

标签: mysql bash parsing while-loop

我有一个包含iana端口列表的文本文件。我想将该文件从bash导入mysql表。但到目前为止,我的专栏文章混杂不清。并且mysql语句最终从文件中插入或读取数据,然后插入到表的错误列中。这是文本文件的外观。这是我尝试导入https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml

的文件的链接
mftp                349        tcp    mftp                         [Dave_Feinleib]                                       [Dave_Feinleib]
mftp                349        udp    mftp                         [Dave_Feinleib]                                       [Dave_Feinleib]
matip-type-a        350        tcp    MATIP Type A                 [Alain_Robert]                                        [Alain_Robert]                                                                      [RFC2351]
matip-type-a        350        udp    MATIP Type A                 [Alain_Robert]                                        [Alain_Robert]                                                                      [RFC2351]
matip-type-b        351        tcp    MATIP Type B                 [Alain_Robert]                                        [Alain_Robert]                                                                      [RFC2351]
matip-type-b        351        udp    MATIP Type B                 [Alain_Robert]                                        [Alain_Robert]                                                                      [RFC2351]
bhoetty             351        tcp    bhoetty                      [John_Kelly]                                          [John_Kelly]                                                                                                                                             This entry records an unassigned but widespread use (added

这是我的剧本

cat service-names.txt | \
while IFS=$'\t' read col1 col2 col3 col4 col5 col6; do 
echo "INSERT INTO ports (id, service, number, proto, description, assign, notes) VALUES (DEFAULT, $col1, $col2, $col3, $col4, $col5, $col6);"
done | sudo mysql -uroot -ppassword dbname

如何正确读取文件...所以我在while循环中为每个变量获取一列...

0 个答案:

没有答案