【问题标题】:bash: write list of file names in a file with additional textsbash:在带有附加文本的文件中写入文件名列表
【发布时间】:2020-08-02 16:15:43
【问题描述】:

我想为文件夹中的每个文件名写入一些文本到文件中。像这样的:

echo "file $(ls *.png) \n duration 0.12" >  test.txt

但是,这段代码只是在文件的开头添加了file,在文件的末尾添加了\n duration 0.12,并在两者之间添加了文件列表,而我想要这样的东西:

  file 1.png
  duration 0.12
  file 2.png 
  duration 0.12

该文件实际上必须是 ffmpeg concat demuxer 格式。

我知道我可以使用循环。但是,我不知道是否有更短的命令,或者循环是否与ls 或其他 linux 命令一样高效?

【问题讨论】:

  • 你真的在尝试使用 ffmpeg concat demuxer?
  • @MarkSetchell 是的,这就是我的目标。
  • 你可以使用printf "%s\nduration 0.12\n" *.png
  • @MarkSetchell 谢谢,这可能是一个答案!但是我猜printf "file %s\nduration 0.12\n" *.png

标签: bash glob ls


【解决方案1】:

您可以使用bash "globbing" 非常简单地做到这一点:

printf "file %s\nduration 0.12\n" *.png

样本输出

file a.png
duration 0.12
file alpha.png
duration 0.12
file b.png
duration 0.12
file bg.png
duration 0.12

请注意,如果您希望它们按特定顺序排列,您可以执行以下操作:

printf "file %s\nduration 0.12\n" d*.png t*.png p*.png

关键字:ffmpeg、concat demuxer、文件和持续时间

【讨论】:

    【解决方案2】:

    使用 GNU 查找:

    find . -maxdepth 1 -name "*.png" -type f -printf "file %f\nduration 0.12\n"
    

    见:man find

    【讨论】:

    • 我就是这个意思:)
    • @Cyrus 欢迎您将我在 cmets 中的建议编辑为您的答案,因为这只是您原始概念的简化?
    • @MarkSetchell:谢谢。我建议将您的评论作为单独的回复发布在上面,因为它使用 bash 的通配符。
    • 请注意,这可能并不总是产生预期的输出。像* 这样的全局对象会按排序顺序展开,而find 则不会。 OP 没有明确要求排序列表,但是,我觉得文件的顺序在这里可能很重要(看起来 OP 想要从图像列表中生成动画)。
    • @Socowi:是的,如果 Ahmad 想要按特定顺序输出,那么这个解决方案就不合适了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2018-11-18
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    相关资源
    最近更新 更多