从文件名中提取唯一的ID [英] Extracting unique id from file name

查看:184
本文介绍了从文件名中提取唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将它们放置在子目录中组织目录中的文本文件。子目录名称从原来的文件名的。以这种方式,将是很容易知道哪些文件属于其相应的文件夹。 for循环bash的 通过所有的txt文件迭代,并创建相应的文件夹。该文本文件具有以下示例格式: XXXX-test_file1-AA1-a2.txt XXXX-test_file1-aa1--2.txt 。根据第一个例子,要创建的只有两件事,对于有关的名称无关紧要的文件夹是 XXXX -aa1-A2 (总是在最后一个6个字符唯一ID)。因此,新的文件夹将被命名为 XXXX-AA1-A2 。以下仅适用于某些文件和休息与其他提取正确的名称。

  FILE_PATH =/ my_files /
在$ FILE_PATH/ * txt文件。做
    TMP = $ {#文件* - };头= $ {文件% - $ TMP}
    中期= $ {TMP% - *};尾= $ {TMP#$中旬 - }
    基地=$ {头,,} - $ {尾,,}
    DIR = $ {基地.TXT%}
    MKDIR -p$目录
    MV$文件,$ DIR / $基地
DONE

$ {VAR#preFIX} 扩展为VAR与preFIX值去掉, $ {VAR%后缀} 相应地执行与后缀相同的替换。最后, $ {VAR ,,} 产生价值的小写版本。然后,我们简单地组装您从这些部件所需的文件名结构。

以上的作品,如果该文件只有两个 - XXXX-test_file里面-aaasw1 XXXX-test_file里面-bswb2u

  |  -  ./
| | - XXXX-aaasw1
| | --xxxx-test_file里面,aaasw1.txt
| | - XXXX-bswb2u
        | --xxxx-test_file里面-bswb2u.txt。

但它打破,如果文件中有两个以上的 - XXXX-test_file里面-CAA-v3u XXXX-test_file里面-CAA-V3 -

  |  -  ./
| | - XXXX-v3u
| | - XXXX-test_file里面-CAA-v3u.txt
| | - XXXX-
        | - XXXX-test_file里面-CAA-V3-.TXT。


解决方案

所以,你希望被命名的目录AB,其中一个就是一切到第一个破折号和b是最后的冲刺和第一之间的一切点?

 触摸XXXX-test_file里面,aaasw1
触摸XXXX-test_file里面,bswb2u
触摸XXXX-test_file里面-CAA-v3u.txt
触摸XXXX-test_file里面-CAA-V3-.TXT对于f *中

    头= $(切-f1 -d'-'<<<$ F)
     中期= $(切-f2 -d'-'<<<$ F)
    尾= $(切-f3- -d​​'-'<<<$ F|切-f 1 -d)。
     EXT = $(切-f3- -d​​'-'<<<$ F|切-f 2- -d)。
    回声[$头] [$中旬] [$尾] [$ EXT]
    MKDIR$ {头} - $ {}尾巴
    MV$ {F}$ {头} - $ {尾} / $ {头} - $ {尾} $ {}分机。
    回声$ {}中旬> $ {头} - $ {}尾/ title_info.txt
DONE树

输出:

  |  -  XXXX-aaasw1
| ` - XXXX-test_file里面,aaasw1
| - XXXX-bswb2u
| ` - XXXX-test_file里面,bswb2u
| - XXXX-CAA-V3-
| ` - XXXX-test_file里面-CAA-V3-.TXT
` - XXXX-CAA-v3u
    ` - XXXX-test_file里面-CAA-v3u.txt

有其他几种方式去了解这一点,但我能想到的那些比这个简单,但并不十分有效,方法更隐蔽。

I am organizing text files in a directory by placing them in subdirectories. The subdirectory name is derived from the original file name. In this way it would be easy to tell which file belongs to its respective folder. The bash for loop iterates through all the txt files and creates the folder accordingly. The text files have the following example format: xxxx-test_file1-aa1-a2.txt or xxxx-test_file1-aa1--2.txt. Based on the first example, the only two things that matter for the name of the about to be created folder is xxxx and -aa1-a2(always have a 6 character unique id at the end). Therefore the new folder would be named xxxx-aa1-a2. The below only works for extracting the right name for certain files and breaks with other.

FILE_PATH="/my_files/"
for file in "$FILE_PATH"/*.txt; do
    tmp=${file#*-}; head=${file%-"$tmp"}
    mid=${tmp%-*}; tail=${tmp#"$mid"-}
    base="${head,,}-${tail,,}"
    dir=${base%.txt}
    mkdir -p "$dir"
    mv "$file" "$dir/$base"
done

${var#prefix} expands to the value of var with prefix removed, and ${var%suffix} correspondingly performs the same substitution with a suffix. Finally, ${var,,} produces the lowercase version of the value. Then we simply assemble the file name structure you want from those parts.

The above works if the file only has two -: xxxx-test_file-aaasw1 or xxxx-test_file-bswb2u

|-- ./
|   |-- xxxx-aaasw1
|       |--xxxx-test_file-aaasw1.txt
|   |-- xxxx-bswb2u
        |--xxxx-test_file-bswb2u.txt.

But it breaks if the file has more than two -:xxxx-test_file-caa-v3u or xxxx-test_file-caa-v3-

|-- ./
|   |-- xxxx-v3u
|       |-- xxxx-test_file-caa-v3u.txt
|   |-- xxxx-
        |-- xxxx-test_file-caa-v3-.txt.

解决方案

So, you want the directory to be named "a-b", where a is everything up to the first dash and b is everything between the last dash and the first dot?

touch xxxx-test_file-aaasw1
touch xxxx-test_file-bswb2u
touch xxxx-test_file-caa-v3u.txt
touch xxxx-test_file-caa-v3-.txt

for f in *
do
    head=$(cut -f1  -d'-' <<< "$f")
     mid=$(cut -f2  -d'-' <<< "$f")
    tail=$(cut -f3- -d'-' <<< "$f" | cut -f 1 -d .)
     ext=$(cut -f3- -d'-' <<< "$f" | cut -f 2- -d .)
    echo "[$head][$mid][$tail][$ext]"
    mkdir "${head}-${tail}"
    mv "${f}" "${head}-${tail}/${head}-${tail}.${ext}"
    echo "${mid}" > "${head}-${tail}"/title_info.txt
done

tree

Outputs:

|-- xxxx-aaasw1
|   `-- xxxx-test_file-aaasw1
|-- xxxx-bswb2u
|   `-- xxxx-test_file-bswb2u
|-- xxxx-caa-v3-
|   `-- xxxx-test_file-caa-v3-.txt
`-- xxxx-caa-v3u
    `-- xxxx-test_file-caa-v3u.txt

There are several other ways to go about this, but the ones I can think of are more cryptic than this straightforward, but not terribly efficient, approach.

这篇关于从文件名中提取唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆