递归串联在一个目录树(加盟)和重命名文本文件 [英] Recursively concatenating (joining) and renaming text files in a directory tree

查看:170
本文介绍了递归串联在一个目录树(加盟)和重命名文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Mac OS X Lion的。

I am using a Mac OS X Lion.

我有一个文件夹:资料结构如下:

I have a folder: LITERATURE with the following structure:

LITERATURE > Y > YATES, DORNFORD > THE BROTHER OF DAPHNE:
  Chapters 01-05.txt
  Chapters 06-10.txt
  Chapters 11-end.txt

我要递归串接分割成多个文件的章节(不是全部都是)。然后,我想写级联文件到其父的父目录。级联文件的名称应该是相同的父目录的名称。

I want to recursively concatenate the chapters that are split into multiple files (not all are). Then, I want to write the concatenated file to its parent's parent directory. The name of the concatenated file should be the same as the name of its parent directory.

例如,运行脚本(在上面所示的文件夹结构),我应该得到以下后。

For example, after running the script (in the folder structure shown above) I should get the following.

LITERATURE > Y > YATES, DORNFORD:
  THE BROTHER OF DAPHNE.txt
  THE BROTHER OF DAPHNE:
    Chapters 01-05.txt
    Chapters 06-10.txt
    Chapters 11-end.txt

在这个例子中,父目录是达芙妮的兄弟和家长的父目录耶茨,DORNFORD

In this example, the parent directory is THE BROTHER OF DAPHNE and the parent's parent directory is YATES, DORNFORD.

[更新3月6日,改写了问题/答案,这样的问题/答案很容易找到和理解。]

[Updated March 6th—Rephrased the question/answer so that the question/answer is easy to find and understand.]

推荐答案

目前还不清楚你所说的递归的意思,但这应该足以让你开始。

It's not clear what you mean by "recursively" but this should be enough to get you started.

#!/bin/bash

titlecase () {  # adapted from http://stackoverflow.com/a/6969886/874188
    local arr
    arr=("${@,,}")
    echo "${arr[@]^}"
}

for book in LITERATURE/?/*/*; do
    title=$(titlecase ${book##*/})
    for file in "$book"/*; do
        cat "$file"
        echo
    done >"$book/$title"
    echo '# not doing this:' rm "$book"/*.txt
done

这循环通过资料 / 的初始 / 作者 / 书名的,并创建文件书名在每本书的目录链中的文件(应该在哪里的空间加?)。 (我会生成它的父目录,然后完全删除这本书的目录,假设它包含任何价值的任何更长的时间。)没有递归,仅仅过了这个目录结构的循环。

This loops over LITERATURE/initial/author/BOOK TITLE and creates a file Book Title (where should a space be added?) from the catenated files in each book directory. (I would generate it in the parent directory and then remove the book directory completely, assuming it contains nothing of value any longer.) There is no recursion, just a loop over this directory structure.

删除章节文件是一个有点冒险,所以我不是在这里做。你可以删除回声 preFIX从行后的第一个完成来启用它。

Removing the chapter files is a bit risky so I'm not doing it here. You could remove the echo prefix from the line after the first done to enable it.

如果您有包含一个星号或其他一些shell元字符,这将是相当更加复杂书的名字 - 在标题分配假定您可以用书名加引号。

If you have book names which contain an asterisk or some other shell metacharacter this will be rather more complex -- the title assignment assumes you can use the book title unquoted.

只有以案例参数扩展转换超出猛砸非常基本的。该阵列操作或许也是,如果你是一个有点吓人一个完整的初学者。引用正确的认识往往也是新人的一个挑战。

Only the parameter expansion with case conversion is beyond the very basics of Bash. The array operations could perhaps also be a bit scary if you are a complete beginner. Proper understanding of quoting is also often a challenge for newcomers.

这篇关于递归串联在一个目录树(加盟)和重命名文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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