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

时间:2019-10-28 19:12:15

标签: linux cat xargs

背景

我有一个public static void ValidateHandler<T>(IAddHandler<T> handler) { handler.GetData(); var result = handler.Add(); if (typeof(T) == typeof(StringModel)) { WriteResults(result as StringModel); } else if (typeof(T) == typeof(IntegerModel)) { WriteResults(result as IntegerModel); } } 这样的文件名:

file

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

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

这应该是一个非常简单的操作...但是我想尝试的一系列命令似乎在逻辑上似乎没有道理:

尝试的解决方案

  1. 将猫放在触摸的地方:./something.txt.docx ./another.cpp.docx ./whoa.cxx.docx

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

  2. 将猫放入xargs触摸中:cat file | touch <not sure what to put here>.docx

    a。我的意图是使用-I option从字面上附加.docx以获得上面看到的结果。以下是-I的预期功能:

      

    -我替换-str

         

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

    这引发了错误cat file | xargs -I{} touch {}.docx

问题

如何循环浏览文件并为文件中的每一行创建相应的.docx?

2 个答案:

答案 0 :(得分:1)

假设文件内容格式合理,并且没有奇怪的特殊情况,您可以执行以下操作:

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

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

答案 1 :(得分:1)

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

ls -1rt | awk'{print“ touch” $ 0“ .docx”}'|重击