Bash - 将数字移动到特定的字符串位置

时间:2016-03-22 10:11:21

标签: bash numbers location output move

我正在尝试了解如何更改脚本以使输出文件不是TEST.docTEST.doc1TEST.doc12等,而是{{1} },TEST.docTEST_1.doc等。更简单地说,就在特定的已知TEST_2.doc之前。(.extension)。

我该怎么做?

这里是剧本

txt|rtf|pages|numbers|keynote|doc|docx|xls|ppt|pdf

修改

#!/bin/bash

rename_files() {
   title="${1##*${2} - }"
   for filename in "$1/"*.*; do
   case "${filename##*.}" in
     txt|rtf|pages|numbers|keynote|doc|docx|xls|ppt|pdf)
       mkdir -p -m 777 "/Users/Desktop/Documents Share/Downloaded/${title}"
       new_path="/Users/Desktop/Documents Share/Downloaded/${title}/${title}.${filename##*.}"
       let "iters=1"
       while [ -f "$new_path" ] ; do
          new_path=$new_path"$iters"
          let "iters++"
       done
       echo "moving $filename -> $new_path"
       mv "${filename}" "${new_path}"
       ;;
   esac
   done
}

rename_category() {
  for path in "/Users/Desktop/Documents Share/Downloads/${1}"*; do
    rename_files "$path" "$1"
  done
}

rename_category DOC

在:

fromdir="/Users/Desktop/Downloads/"  
todir="/Users/Desktop/Documents Share/Downloaded/${2}/"

rename_files() {
   title="${1##*${2} - }"
   let "iters=0"
   for filename in "$1/"*.*; do
     case "${filename##*.}" in txt|rtf|pages|numbers|keynote|doc|docx|xls|ppt|pdf)
         new_path="${todir}/${2}/${title}.${filename##*.}"
            new_path=$new_path  # removed "$iters"
            let "iters++"
            # extract $path without dot + extension
            new_path_basename=${new_path%*.*};
            # Extract extension from $path
            ext=${new_path##*.};
            echo "moving $filename -> ${new_path_basename}_${iters}.${ext}"
            # Add ${iters} before extension
            mv "${filename}" "${new_path_basename}_${iters}.${ext}"
         ;;
     esac
   done
}

rename_category() {
  for path in "${fromdir}${1}"*; do
    rename_files "$path" "$1"
  done
}

rename_category DOC
rename_category PRJ

后:

/Users/Desktop/Downloads/DOC - TESTDOC/AFile.doc
                                      /AnotherFile.doc

/Users/Desktop/Downloads/PRJ - TESTPRJ/AFile.pdf
                                      /AnotherFile.pdf

EDIT2

有两个测试文件: /Users/Desktop/Documents Share/Downloaded/DOC/TESTDOC_1.doc /TESTDOC_2.doc /Users/Desktop/Documents Share/Downloaded/PRJ/TESTPRJ_1.pdf /TESTPRJ_2.pdf big_buck_bunny_240p_50mb.mp4

输出日志

big_buck_bunny_240p_50mb copy.mp4

EDIT3

脚本的最后更新(不是“特殊”)

sh Script.sh

moving /Users/Desktop/Downloads/MOV - TEST/big_buck_bunny_240p_50mb copy.mp4 -> /Users/Desktop/Documents Share/Downloaded/MOV/TEST_1.mp4

cp /Users/Desktop/Downloads/MOV - TEST/big_buck_bunny_240p_50mb copy.mp4 /Users/Desktop/Documents Share/Downloaded/MOV/TEST_1.mp4

moving /Users/Desktop/Downloads/MOV - TEST/big_buck_bunny_240p_50mb.mp4 -> /Users/Desktop/Documents Share/Downloaded/MOV/TEST_2.mp4

cp /Users/Desktop/Downloads/MOV - TEST/big_buck_bunny_240p_50mb.mp4 /Users/albertorapolla/Desktop/Documents Share/Downloaded/MOV/TEST_2.mp4

1 个答案:

答案 0 :(得分:1)

试试这个(评论在脚本中):

WHERE CAST(AuditData AS VARCHAR(MAX)) LIKE '% Username="Blondie"%'