【问题标题】:Use a command over a set of files and save results [duplicate]对一组文件使用命令并保存结果[重复]
【发布时间】:2020-01-15 02:17:49
【问题描述】:

我在一个目录中有一组文件

test1.in
test2.in
test3.in
test4.in
...

我已经编写了一个转换工具来从这些文件中提取一些数据并将其保存到另一个文件中。

我想为每个文件运行该转换工具并将结果保存到不同的输出文件中

./tool -i test1.in -i test1.out
./tool -i test2.in -o test2.out

我已经看到很多关于迭代文件并为每个文件运行所需命令的答案,但我不确定如何指定输出。

for file in dir/*.in; do
    ./tool -i file -o ???
done

如何为输出文件命名?

【问题讨论】:

  • 在 bash 手册中阅读有关参数扩展可以做的所有漂亮事情。
  • 你的例子看起来很奇怪!你的意思是你每次都想覆盖test2.out
  • ups,这是一个错字。会改正的。谢谢指出

标签: bash shell scripting


【解决方案1】:
for file in dir/*.in; do
    ./tool -i "$file" -o "${file%.*}".out
done

"${file%.*}"代表$file在最后一个.之前的值(见https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion,寻找${parameter%word})。

【讨论】:

  • 你确定这是正确的吗?我添加了echo 以查看输出文件及其始终输出 in.out
  • 不用等待。我会测试和更新..
  • 完成。我认为这现在有效..
猜你喜欢
  • 2011-07-16
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多