mysql将多行作为单个记录导入

mysql将多行作为单个记录导入,mysql,bash,Mysql,Bash,昨天把这个贴到了Reddit,但是没有爱。我在Centos上,编写bash脚本并解析数据以导入mysql 我必须转换一个故事档案,它将故事的主要部分存储在纯文本文件中,并且需要能够将这些多行文本文件导入数据库中的一列。我知道我可以使用mysqlimport,并且我将文件指定为管道分隔符-但是因为我导入的文本文件中有回车符/换行符,所以它将每个段落作为自己的行导入。因此,当我使用mysqlimport时,一个9段文本文件将作为9行导入 有办法做到这一点吗 我知道用于导入的理想文本文件(带有管道分隔

昨天把这个贴到了Reddit,但是没有爱。我在Centos上,编写bash脚本并解析数据以导入mysql

我必须转换一个故事档案,它将故事的主要部分存储在纯文本文件中,并且需要能够将这些多行文本文件导入数据库中的一列。我知道我可以使用mysqlimport,并且我将文件指定为管道分隔符-但是因为我导入的文本文件中有回车符/换行符,所以它将每个段落作为自己的行导入。因此,当我使用mysqlimport时,一个9段文本文件将作为9行导入

有办法做到这一点吗

我知道用于导入的理想文本文件(带有管道分隔符)将类似于(没有空白行之间):

这是我的记录| 12345

另一项记录| 24353

再吃一个百吉饼,为什么不呢

但是,我的文件实际上更接近于此:

这是我第一段的第一行。现在我要做更多的线包装之类的

这是同一文本文件中的第二行,应与第一行一起作为单个“blob”或文本字段中的单个记录处理|12345


这是从别人掉在我膝上的坏软件中恢复过来的最后一个绊脚石,我希望这可以做到。我有14000个这样的文本文件(每一个都是这种格式的),所以手工操作是不可能的。

将新行编码/传输为“\n”,并使用与“\t”相同的方式选项卡。这是将任何url或原始文本存储到数据库中时的最佳实践。这也将帮助您避免sql注入,并解决您当前的问题


请让我知道这是否有帮助。谢谢。

我不知道将行转换为sql语句时的性能。我认为这是有用的:

输入

剧本

my_insert="INSERT INTO my_table                                                                                
     (field1, field2)                                                                                          
     VALUES                                                                                                    
     ('"                                                                                                       

   firstline=0    
   while read -r line; do
      if [[ -z "${line}" ]]; then
         printf "\n"             
         continue;               
      fi                         
      if [[ "${firstline}" -eq 0 ]]; then
         printf "%s" "${my_insert}"      
         firstline=1                     
      fi                                 
      line_no_pipe=${line%|*}            
      if [[ "${line}" = "${line_no_pipe}" ]]; then
         printf "%s\n" "${line}"                  
      else                                        
         printf "%s',%s);\n" "${line_no_pipe}" "${line##*|}"
         firstline=0                                        
      fi                                                    
   done < input                                             
my_insert="INSERT INTO my_table                                                                                
     (field1, field2)                                                                                          
     VALUES                                                                                                    
     ('"                                                                                                       

   firstline=0    
   while read -r line; do
      if [[ -z "${line}" ]]; then
         printf "\n"             
         continue;               
      fi                         
      if [[ "${firstline}" -eq 0 ]]; then
         printf "%s" "${my_insert}"      
         firstline=1                     
      fi                                 
      line_no_pipe=${line%|*}            
      if [[ "${line}" = "${line_no_pipe}" ]]; then
         printf "%s\n" "${line}"                  
      else                                        
         printf "%s',%s);\n" "${line_no_pipe}" "${line##*|}"
         firstline=0                                        
      fi                                                    
   done < input                                             
INSERT INTO my_table
     (field1, field2)
     VALUES
     ('This is the first line of my first paragraph. And now I'm going to do some more line wrapping and stuff.

This is a second line from the same text file that should be treated as a single record along with the first line in a single "blob" or text field. ',12345);
INSERT INTO my_table
     (field1, field2)
     VALUES
     ('I am hoping I understood the question correct.
Everything without a pipe is part of the first field.
And the line with a pipe is for field 1 and 2.
Like this one ',12346);