我想在bash中做一些特别的事情。我已经找到了for循环和find的部分解决方案,但我更喜欢制作一个脚本来完成更复杂的工作。
我有一个复杂的目录结构如下:
fiction/
book1/
chapter1/
page1.txt
page2.txt
..
chapter2/
page5.txt
page6.txt
..
book2/
chapter1/
chapter2/
..
non-fiction/
book5/
chapter1/
..
我希望你能明白。
简而言之,我想做以下事情:
- 输入每本书和每一章,例如第1章
- 将第1页、第2页等全部放入/mybooks/book1/chapter.txt
- 继续下一章,对/mybooks/book1/chapter2.txt重复
- 继续读下一本书
我用一个简单的脚本做到了这一点:
for i in */; do cat "$i"*.txt > /newdir/"${i%%/}".txt; done
当我意识到有些书不仅分为章节,而且分为包含章节的部分或小节时,我的第一个问题就出现了。唯一的解决方案是cd到这些文件夹并手动执行脚本。
但这不是专业的方式。如何使脚本更智能地遍历子目录,然后再移回?
您可以尝试以下操作:
for b in */*
do
cat $(find "$b" -type f -name *.txt| sort) >> "$b"/$(basename "$b").txtb
done
这个答案。。。
- 处理带有空格的目录
- 如果一章中有超过9页,则对页面进行正确排序
它基于以下假设:
- 页面总是紧挨着第章*
- 页面被命名为page[一个或多个数字].txt
- 路径中只有一个部分与"chapter*"匹配,所以没有什么比得上"chapter2/succapter1"
#!/bin/bash
while read -r -d $' ' 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