我想将目录中的所有文本文件转换为一个 csv 文件。我希望 csv 的输入是由文件名中找到的文本文件的作者标记的文本文件中的文本。作为参考,一些文件名如下所示:
'Winston Churchill___The Crisis, Complete.txt'
'Winston Churchill___The Crossing.txt'
'Winston Churchill___The Dwelling Place of Light, Complete.txt'
'Winston Churchill___The Inside of the Cup, Complete.txt'
'Zane Grey___Betty Zane.txt'
'Zane Grey___Desert Gold.txt'
'Zane Grey___Riders of the Purple Sage.txt'
示例输出如下:
column1 column2
Author1 text written by author 1......
Author1 text written by author 1......
Author2 text written by author 2......
Author2 text written by author 2......
编辑:测试文本...需要作者在相应列中编写的 1000 个字符的文本。
答案1
使用 来ksh93
代替 ,bash
其内置支持csv
以其%#q
printf
格式进行打印(并且更高效$(<file)
):
for file in *___*.txt; do
printf "%#q,%#q\n" "${file%%___*}" "$(<"$file")"
done >> file.csv