【问题标题】:How to insert a line of text into each .obj file in each directory by shell script?如何通过shell脚本在每个目录中的每个.obj文件中插入一行文本?
【发布时间】:2014-02-27 09:00:18
【问题描述】:

我需要插入

mtllib mesh.mtl

我所有的 .obj 文件中的行。目录结构是

Product1/
   product1.obj
   product1.mtl
Product2/
   product2.obj
   product2.mtl

然后我应该将 product*.obj / product*.mtl 分别重命名为 mesh.obj / mesh.mtl。

如何通过 shell 脚本做到这一点?

【问题讨论】:

    标签: shell grep sh


    【解决方案1】:
    for f in Product*/*.obj
    do
      echo "mtllib mesh.mtl" >/tmp/$$
      cat $f >> /tmp/$$
      mv /tmp/$$ $f
    done
    
    for f in Product*/*.obj
    do
      mv $f $(dirname $f)/mesh.obj
    done
    
    for f in Product*/*.mtl
    do
      mv $f $(dirname $f)/mesh.mtl
    done
    

    (运行前做好备份)

    【讨论】:

    • 谢谢。实际上我的文件夹名称不是有序的,它可以是一切。文件名包含空格。
    • 因此您可以使用find 搜索扩展名并包含所有var。指向带双引号的文件名(如"$f"
    猜你喜欢
    • 2012-05-02
    • 2015-09-02
    • 2012-09-24
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    相关资源
    最近更新 更多