繁体   English   中英

如何遍历文件以创建一系列 new.docx 文件?

[英]How do I loop through a file to create a series of new .docx files?

背景

我有一个像这样的file名文件:

something.txt
another.cpp
whoa.cxx
...

我想在当前目录中为每个文件创建一个相应的文本文件:

./something.txt.docx
./another.cpp.docx
./whoa.cxx.docx

这应该是一个非常简单的操作......但是我正在考虑尝试的一系列命令在逻辑上似乎没有意义:

尝试的解决方案

  1. Pipe 一只猫接触: cat file | touch <not sure what to put here>.docx cat file | touch <not sure what to put here>.docx

    一个。 如您所见,我不知道如何将 append a.docx 扩展名添加到我遇到的每个文件名,而且我不知道如何以这种方式巧妙地执行正则表达式 append。 触摸手册页在这方面似乎也没有帮助。

  2. Pipe cat into xargs touch: cat file | xargs -I{} touch {}.docx cat file | xargs -I{} touch {}.docx

    一个。 我的意图是使用-I 选项字面上的 append.docx 来获得上面看到的结果。 以下是 -I 的预期功能:

    -I 替换-str

    用从标准输入读取的名称替换初始参数中出现的 replace-str。 此外,未加引号的空格不会终止输入项; 相反,分隔符是换行符。 暗示 -x 和 -L 1。

    这引发了错误xargs: .docx: No such file or directory

问题

如何遍历文件并为文件中的每一行创建对应的.docx?

假设文件内容的格式合理并且没有奇怪的极端情况,您可以这样做:

while IFS= read -r line; do
  touch "$line.docx"
done < input-file-name.txt

我刚刚确认它可以在我的系统上运行。

这可以通过以下命令来实现

ls -1rt | awk'{打印“触摸”$0“.docx”}'| bash

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM