如何循环访问一个文件以创建一系列新的 .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
这应该是一个非常简单的操作...但是我正在考虑尝试的一系列命令在逻辑上似乎没有意义:
尝试过的解决方案
管一只猫接触:cat file | touch <not sure what to put here>.docx
。
一个。如您所见,我不知道如何将 .docx 扩展名附加到我遇到的每个文件名,而且我不知道如何以这种方式巧妙地进行正则表达式附加。 touch 手册页在这方面似乎也没有帮助。
将 cat 管道化为 xargs touch:cat file | xargs -I{} touch {}.docx
一个。我的意图是使用 -I option 从字面上追加 .docx 以获得上面看到的结果。以下是 -I:
的预期功能
-I replace-str
Replace occurrences of replace-str in the initial-arguments
with names read from standard input. Also, unquoted blanks do
not terminate input items; instead the separator is the
newline character. Implies -x and -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 '{ print "touch " $0 ".docx" }' | bash
背景
我有一个 file
个文件名,如下所示:
something.txt
another.cpp
whoa.cxx
...
我想为当前目录中的每一个创建一个相应的文本文件:
./something.txt.docx
./another.cpp.docx
./whoa.cxx.docx
这应该是一个非常简单的操作...但是我正在考虑尝试的一系列命令在逻辑上似乎没有意义:
尝试过的解决方案
管一只猫接触:
cat file | touch <not sure what to put here>.docx
。一个。如您所见,我不知道如何将 .docx 扩展名附加到我遇到的每个文件名,而且我不知道如何以这种方式巧妙地进行正则表达式附加。 touch 手册页在这方面似乎也没有帮助。
将 cat 管道化为 xargs touch:
cat file | xargs -I{} touch {}.docx
一个。我的意图是使用 -I option 从字面上追加 .docx 以获得上面看到的结果。以下是 -I:
的预期功能-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -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 '{ print "touch " $0 ".docx" }' | bash