0

我想在 bash 中做一些特别的事情。我找到了 for 循环和 find 的部分解决方案,但我更喜欢编写脚本来完成更复杂的工作。

我有一个复杂的目录结构如下:

fiction/
 book1/
  chapter1/
   page1.txt
   page2.txt
   ..
  chapter2/
   page5.txt
   page6.txt
  ..
 book2/
  chapter1/
  chapter2/
 ..
non-fiction/
 book5/
  chapter1/
..

我希望你明白这一点。

简而言之,我想做以下事情:

  1. 输入每本书和章节,例如 Chapter1
  2. cat /mybooks/book1/chapter1.txt 中的所有 page1、page2 等
  3. 继续下一章,并重复 /mybooks/book1/chapter2.txt
  4. 继续阅读下一本书

我设法用简单的脚本做到了这一点:

for i in */; do cat "$i"*.txt > /newdir/"${i%%/}".txt; done

我的第一个问题出现了,因为我意识到有些书不仅按章节划分,而且还分为包含章节的部分或小节。唯一的解决方案是 cd 到这些文件夹并手动执行脚本。

但这不是专业的方式。如何使脚本更智能地遍历子目录然后返回?

4

2 回答 2

0

您可以尝试以下方法:

for b in */*
do 
  cat $(find "$b" -type f -name \*.txt| sort) >> "$b"/$(basename "$b").txtb
done
于 2012-09-02T20:59:19.720 回答
0

这个答案...

  • 处理带空格的目录
  • 如果您在一章中有超过 9 页,则对页面进行正确排序

它基于以下假设:

  • 页面总是紧接在章节的下方*
  • 页面被命名为 page[一位或多位].txt
  • 只有一个部分与路径中的“chapter*”匹配,所以没有像“chapter2/subchapter1”这样的部分

#!/bin/bash

while read -r -d $'\0' path; do
    output="$path".txt
    ls -1 "$path"/page*.txt | \
     sed 's/^.*\([0-9][0-9]*\).txt/\1/' | \
     sort -n | \
     while read n; do
         echo "cat ${path}/page${n}.txt >> $output"
         # cat "${path}/page${n}.txt" >> "$output"
     done
done < <(find fiction/ -type d -name "chapter*" -print0)

如果对结果满意,请删除 - 行echo并取消注释下面的行。

例子:

find f
f
f/book2
f/book2/chapter1
f/book2/chapter1/page10.txt
f/book2/chapter1/page2.txt
f/book2/chapter1/page1.txt
f/book2/chapter3
f/book2/chapter3/page2.txt
f/book2/chapter3/page1.txt
f/book2/chapter2
f/book2/chapter2/page2.txt
f/book2/chapter2/page1.txt
f/book2/part1
f/book2/part1/subsection1
f/book2/part1/subsection1/chapter1
f/book2/part1/subsection1/chapter1/page2.txt
f/book2/part1/subsection1/chapter1/page3.txt
f/book2/part1/subsection1/chapter1/page1.txt
f/book1
f/book1/chapter1
f/book1/chapter1/page2.txt
f/book1/chapter1/page1.txt
f/book1/chapter3
f/book1/chapter3/page2.txt
f/book1/chapter3/page1.txt
f/book1/chapter2
f/book1/chapter2/page2.txt
f/book1/chapter2/page1.txt
f/book with space
f/book with space/chapter1
f/book with space/chapter1/page2.txt
f/book with space/chapter1/page1.txt

输出:

cat f/book2/chapter1/page1.txt >> f/book2/chapter1.txt
cat f/book2/chapter1/page2.txt >> f/book2/chapter1.txt
cat f/book2/chapter1/page10.txt >> f/book2/chapter1.txt
cat f/book2/chapter3/page1.txt >> f/book2/chapter3.txt
cat f/book2/chapter3/page2.txt >> f/book2/chapter3.txt
cat f/book2/chapter2/page1.txt >> f/book2/chapter2.txt
cat f/book2/chapter2/page2.txt >> f/book2/chapter2.txt
cat f/book2/part1/subsection1/chapter1/page1.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book2/part1/subsection1/chapter1/page2.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book2/part1/subsection1/chapter1/page3.txt >> f/book2/part1/subsection1/chapter1.txt
cat f/book1/chapter1/page1.txt >> f/book1/chapter1.txt
cat f/book1/chapter1/page2.txt >> f/book1/chapter1.txt
cat f/book1/chapter3/page1.txt >> f/book1/chapter3.txt
cat f/book1/chapter3/page2.txt >> f/book1/chapter3.txt
cat f/book1/chapter2/page1.txt >> f/book1/chapter2.txt
cat f/book1/chapter2/page2.txt >> f/book1/chapter2.txt
cat f/book with space/chapter1/page1.txt >> f/book with space/chapter1.txt
cat f/book with space/chapter1/page2.txt >> f/book with space/chapter1.txt
于 2012-09-02T21:38:16.500 回答