【问题标题】:linux create a file with the titles of the files inside a csvlinux在csv中创建一个带有文件标题的文件
【发布时间】:2019-03-19 20:57:02
【问题描述】:

所以我有一个包含文件名列表的文件,这个文件是 .csv

filename1

filename2

filename3...

等等(有数百个)

我想要做的是将此列表输出到他们自己的文件中,与此列表所在的位置相同,并将它们制作成 .pdf 文件

所以基本上文件夹会有

Filenamelist.csv

filename1.pdf

filename2.pdf

filename3.pdf

非常感谢您对此的任何见解或帮助!

编辑:

这是我的sn-p代码

#!/bin/bash 
if [[-f "/mnt/c/users/jesse/desktop/test/list.csv"]] 
then 
while IFS='|' read -r pdfid 
do 
touch "/mnt/c/users/jesse/desktop/test/${pdfid}.pdf" 
done 
fi

我在尝试 bash 时遇到错误

syntax error near unexpected token `fi

谁能帮我解决这个错误?

我在 windows 上使用 ubuntu。

【问题讨论】:

  • 很难说你的代码有什么问题,因为你没有提供它或者你遇到的错误。另见How to create a Minimal, Complete, and Verifiable example
  • 我得到的错误是 '#!/bin/bash if [[-f "/mnt/c/users/jesse/desktop /test/list.csv"]] 然后当 IFS='|' read -r pdfid do #touch "/mnt/c/users/jesse/desktop/test/${pdfid}.pdf" echo ${pdfid} done fi'
  • 请将您的代码和结果添加到您的问题中。
  • 会的,抱歉

标签: linux list csv output


【解决方案1】:

您可以以迭代方式使用read 命令,如下所述:https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/

【讨论】:

  • 这将允许您阅读创建文件所需的内容。在每次迭代中,您只需 touch 文件,用字符串 '.pdf' 插入变量
  • 对不起,我对 stackoverflow 还很陌生,评论有点奇怪。是的,这看起来正是我想要它做的。我只是将该命令复制并粘贴到窗口中还是应该制作一个 bash 脚本?
  • 将其粘贴到可执行文件中会更容易调试,但您也可以这样做。首先只输出行,然后在最后输出与“.pdf”连接的行。最后,不要将文本回显到标准输出,而是使用touch 来实际创建文件。
【解决方案2】:

这行得通吗?

if [[ -f "/mnt/c/users/jesse/desktop/test/list.csv"]]

then

while IFS='|' read -r pdfid

do

touch "/mnt/c/users/jesse/desktop/test/pdfid.pdf"

done < "/mnt/c/users/jesse/desktop/test/list.csv"

fi

【讨论】:

  • 你会想要使用内插字符串,所以,把触摸变成这样:touch "/mnt/c/users/jesse/desktop/test/${pdfid}.pdf"。作为一般规则,作为开发过程的一部分,我会从 echo 而不是 touch 开始,以调试我的脚本以确保它正常工作。
  • 感谢您的意见!我正在尝试 bash 这个脚本#!/bin/bash if [[-f "/mnt/c/users/jesse/desktop/test/list.csv"]] then while IFS='|' read -r pdfid do #touch "/mnt/c/users/jesse/desktop/test/${pdfid}.pdf" echo ${pdfid} done fi 并且我收到第 9 行的语法错误(意外标记 `fi' 附近的语法错误)关于发生了什么的任何想法?
猜你喜欢
  • 1970-01-01
  • 2017-10-15
  • 2019-07-21
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多