如何更新具有多行值(其中文件名是 id)的 MySQL 表列?
我的目录中有数百个文件。我将其称为 /tmp/testfiles/
目录 /tmp/testfiles/
中的文件包含多行文本。例如,通常猫filename.txt
会导致
步骤1)去那里找到它
步骤2)如果你在那里找不到它,试着看看那里的某个地方
bla bla bla
更多bla bla bla
文件名是这些文件的 ID。例如,如果文件名是 1000.txt,则 1000 是参考 ID。
所以我希望将这些文件的内容输出到现有表的一列中,其中文件名是 id 号。
到目前为止,我得到的是
1) 使用 sed 在行的每一端插入 /n,然后使用 tr 删除行
for file in `find *txt | cut -d "." -f1`
do
sed -e 's/$/\\n/' ${file}.txt > ${file}.tmp; cat ${file}.tmp | tr '\n' ' ' > ${file}.txt; \rm ${file}.tmp;sleep 2
done
2) 使用 sed 添加 $filename;在文件每一行的开头
for file in `find *txt | cut -d "." -f1`
do
sed -e 's/^/'${file}';"/' -e 's/$/"/' ${file}.txt > ${file}.tmp; \mv ${file}.tmp ${file}.txt;sleep 2
done
如果您正在阅读到目前为止,简而言之......我正在尝试将目录中的所有文件输出到 csv 中。然后我可以对我的 MySQL 表执行 LOAD INFILE。
所以我有这样的东西
filename1; "Step 1) Go find this there /n /n Step 2) If you cant find it there, try and take a look somewhere over there /n /n bla bla bla"
filename2; "Step 1) a second line for example purposes /n /n Step 2) i reckon a third line is not needed /n /n bla bla bla"
所以这是我的问题
- 如何使用 LOAD INFILE 仅当 id = filename 时更新现有的列?
- 还有其他方法将数据传输到MySQL吗?
I have a few hundreds file in a directory. I'm calling it /tmp/testfiles/
The files in the directory /tmp/testfiles/
contain multiple lines of text. for e.g Typically a cat filename.txt
would result in
Step 1) Go find this there
Step 2) If you cant find it there, try and take a look somewhere over there
bla bla bla
some more bla bla bla
The filenames are the id for these files. for e.g if the filename is 1000.txt then 1000 is the reference id.
So I'm hoping to output the content of these files into a column of an already existing table where the filename is the id number.
So far, here what I've got
1) use sed to insert /n at each end of line and then use tr to remove the lines
for file in `find *txt | cut -d "." -f1`
do
sed -e 's/$/\\n/' ${file}.txt > ${file}.tmp; cat ${file}.tmp | tr '\n' ' ' > ${file}.txt; \rm ${file}.tmp;sleep 2
done
2) use sed to add $filename; at the beginning of each line of the files
for file in `find *txt | cut -d "." -f1`
do
sed -e 's/^/'${file}';"/' -e 's/$/"/' ${file}.txt > ${file}.tmp; \mv ${file}.tmp ${file}.txt;sleep 2
done
If you're reading this far, in a nutshell.. Im trying to output all the files in the directory into a csv. Which then i can do a LOAD INFILE to my MySQL table.
so i have something like this
filename1; "Step 1) Go find this there /n /n Step 2) If you cant find it there, try and take a look somewhere over there /n /n bla bla bla"
filename2; "Step 1) a second line for example purposes /n /n Step 2) i reckon a third line is not needed /n /n bla bla bla"
So here's my question(s)
- How do i use LOAD INFILE to update the column of an existing only when id = filename ?
- Are there other ways to transfer the data to MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
换行符对我来说似乎有点奇怪。
我会这样做:
考虑一下您可以使用以下命令将文件转换为其十六进制表示形式
<前><代码>df | od -tx1 |切-c9- | tr -d ' ' | tr -d '\n'
使用此命令将结果放入构建查询的脚本中:
将脚本的输出通过管道传送到
mysql
CLI。The thing with the newlines seems somewhat peculiar to me.
I would do it this way:
Consider that you can transform a file into its hex representation with
Use this to put the result in a script building a query:
Pipe the output of the script into the
mysql
CLI.