代码之家  ›  专栏  ›  技术社区  ›  MatthijsG

使用文本文件作为源重命名文件

  •  2
  • MatthijsG  · 技术社区  · 9 年前

    标题的内容。txt看起来像这样。在数字和标题之间有一个标签(可以随意更改)。

    001 title of 1
    002 this is 2
    003 and here goes 3
    004 number four
    005 hi this is five
    etc
    

    文件夹的内容如下所示。没有例外。

    file_001.ext
    file_002.ext
    file_003.ext
    file_004.ext
    file_005.ext
    etc
    
    1 回复  |  直到 9 年前
        1
  •  4
  •   Eduard Itrich    9 年前

    只需使用 read ,使用 awk cut (谢谢你,杰克)和 mv ./filenames

    #!/bin/bash
    while IFS='' read -r line || [[ -n "$line" ]]; do
            NR=$(echo "$line" | cut -f 1)
            NAME=$(echo "$line" | cut -f 2)
            if [ -f "foo_${NR}.ext" ] ; then
                    mv "foo_${NR}.ext" "$NAME"
            fi
    done < filenames
    
    推荐文章