用于将 .txt 文件插入 csv 的 Bash 脚本

用于将 .txt 文件插入 csv 的 Bash 脚本

我正在寻找一个脚本,它可以让我能够将一堆.txt文件编译成.csv参考文件。

我需要将.txt文件名作为第一个 A 字段,并将文件内容.txt放在 B 字段中。有人有什么建议吗?

这是我之前用过的脚本, -d, "./*.txt >> test.csv

我对脚本编写相当陌生,并将使用 Lynda 进一步学习,但任何帮助都会很棒。谢谢大家。

答案1

我将当前日期回显到以当前时间纳秒值命名的文件中。然后我运行了 for 循环,如下所示。上面的另一位评论者在询问单行时是准确的。换行符和其他控制字符可能会影响 CSV 的显示方式。如果需要,可以使用tr来删除控制字符。

请注意,内部双引号已被转义。

# for file in *.txt ; do echo -e "\"$file\",\"$(cat $file)\"" ; done > DudesFile.csv
# cat DudesFile.csv
"522846064.txt","Mon Apr 30 16:38:07 EDT 2018"
"528189011.txt","Mon Apr 30 16:38:08 EDT 2018"
"534462603.txt","Mon Apr 30 16:38:09 EDT 2018"
"539602831.txt","Mon Apr 30 16:38:10 EDT 2018"
"544644085.txt","Mon Apr 30 16:38:11 EDT 2018"
"549794326.txt","Mon Apr 30 16:38:12 EDT 2018"
"554869915.txt","Mon Apr 30 16:38:13 EDT 2018"
"559829685.txt","Mon Apr 30 16:38:14 EDT 2018"
"564845609.txt","Mon Apr 30 16:38:15 EDT 2018"
"570065970.txt","Mon Apr 30 16:38:16 EDT 2018"
"575184986.txt","Mon Apr 30 16:38:17 EDT 2018"
"580279560.txt","Mon Apr 30 16:38:18 EDT 2018"
"585486035.txt","Mon Apr 30 16:38:19 EDT 2018"
"590751832.txt","Mon Apr 30 16:38:20 EDT 2018"
"596243540.txt","Mon Apr 30 16:38:21 EDT 2018"
"601355464.txt","Mon Apr 30 16:38:22 EDT 2018"
"606470579.txt","Mon Apr 30 16:38:23 EDT 2018"
"611651121.txt","Mon Apr 30 16:38:24 EDT 2018"
"616854098.txt","Mon Apr 30 16:38:25 EDT 2018"
"622111044.txt","Mon Apr 30 16:38:26 EDT 2018"
"627396500.txt","Mon Apr 30 16:38:27 EDT 2018"
"632593596.txt","Mon Apr 30 16:38:28 EDT 2018"
"637756851.txt","Mon Apr 30 16:38:29 EDT 2018"
"643109987.txt","Mon Apr 30 16:38:30 EDT 2018"
"648373987.txt","Mon Apr 30 16:38:31 EDT 2018"
"653597441.txt","Mon Apr 30 16:38:32 EDT 2018"
"658773675.txt","Mon Apr 30 16:38:33 EDT 2018"
"663844298.txt","Mon Apr 30 16:38:34 EDT 2018"
"668910760.txt","Mon Apr 30 16:38:35 EDT 2018"
"674208819.txt","Mon Apr 30 16:38:36 EDT 2018"
"679508914.txt","Mon Apr 30 16:38:37 EDT 2018"
"684725670.txt","Mon Apr 30 16:38:38 EDT 2018"
"689989415.txt","Mon Apr 30 16:38:39 EDT 2018"
"695392140.txt","Mon Apr 30 16:38:40 EDT 2018"
"700430922.txt","Mon Apr 30 16:38:41 EDT 2018"
"705658665.txt","Mon Apr 30 16:38:42 EDT 2018"
"711117890.txt","Mon Apr 30 16:38:43 EDT 2018"
"716275104.txt","Mon Apr 30 16:38:44 EDT 2018"
"721507284.txt","Mon Apr 30 16:38:45 EDT 2018"

相关内容