![将 fb2 转换为 txt](https://linux22.com/image/1122117/%E5%B0%86%20fb2%20%E8%BD%AC%E6%8D%A2%E4%B8%BA%20txt.png)
仍有许多设备无法读取 fb2 但可以读取 txt。此外,与未压缩的 fb2 相比,txt 版本占用的空间要少得多。
答案1
虽然 calibre 可以做到这一点,但安装它需要 >100M,并且unoconv
如果您有 LibreOffice/OpenOffice,则会安装大多数组件。
sudo apt-get install unoconv --no-install-recommends
unoconv -f txt *.fb2
答案2
有时你只需要将 fb2 转换为 txt。下面是我用来做这件事的 3 个 bash 脚本递归地针对当前目录内的所有目录。请注意,这些脚本将删除原始 fb2 文件,因此请在数据副本上运行它。
- 安装依赖项:
sudo apt-get install uchardet dos2unix unoconv
- 使用此命令可以递归地将任何指定类型的文档转换为 txt:
./finder.sh ./convert_any_to_txt.sh fb2 fb2.zip doc docx odt
finder.sh
:
# Find files and process them using an external command.
# Usage:
# ./finder.sh ./processing_script.sh txt fb2 fb2.zip doc docx
counter=0
find_results=()
for ext in "${@:2}"
do
# @see https://stackoverflow.com/a/54561526/10452175
readarray -d '' ext_results < <(find . -type f -name "*.${ext}" -print0)
for file in "${ext_results[@]}"
do
counter=$((counter+1))
find_results+=("${file}")
echo ${counter}") ${file}"
done
done
countOfResults=$((counter))
echo -e "Found ${countOfResults} files.\n"
echo "Processing..."
counter=0
for file in "${find_results[@]}"
do
counter=$((counter+1))
echo -n ${counter}"/${countOfResults}) "
eval "$1 '${file}'"
done
echo "All files have been processed."
convert_any_to_txt.sh
:
# This script converts a single fb2 to UTF-8/LF txt.
#
# Usage to convert all fb2 to txt recursively:
# ./finder.sh ./convert_any_to_txt.sh fb2 fb2.zip doc docx odt
#
# You should install some dependencies before:
# sudo apt-get install uchardet dos2unix unoconv
FILE_ORIGINAL=$(readlink -f "$1")
FILE_ORIGINAL_EXT="${FILE_ORIGINAL##*.}"
FILE_ORIGINAL_WITHOUT_EXT="${FILE_ORIGINAL%.*}"
FORMAT_NEW="txt"
FILE_NEW="${FILE_ORIGINAL_WITHOUT_EXT}.${FORMAT_NEW}"
echo "${FILE_ORIGINAL_EXT}: ${FILE_ORIGINAL}"
# Add "--no-launch" argument to increase the speed, but risk to fail on some file:
# unoconv --no-launch -f "${FORMAT_NEW}" "${FILE_ORIGINAL}" || exit 1
unoconv -f "${FORMAT_NEW}" "${FILE_ORIGINAL}" || exit 1
# Process given txt:
./convert_txt_to_utf8_lf.sh "${FILE_NEW}"
rm "${FILE_ORIGINAL}"
convert_txt_to_utf8_lf.sh
:
# This script converts a single file to UTF-8 and sets LF line endings.
#
# Use following command to find and convert all txt files recursively:
# ./finder.sh ./convert_txt_to_utf8_lf.sh txt
#
# You should install some dependencies before:
# sudo apt-get install uchardet dos2unix unoconv
FILE_ORIGINAL="$1"
FILE_TEMP="${FILE_ORIGINAL}.temp"
FILE_NEW="${FILE_ORIGINAL}"
ORIGINAL_ENCODING=`uchardet "${FILE_ORIGINAL}"`
NEW_ENCODING="utf-8"
echo "${ORIGINAL_ENCODING}: ${FILE_ORIGINAL}"
iconv -f $ORIGINAL_ENCODING -t $NEW_ENCODING "${FILE_ORIGINAL}" -o "${FILE_TEMP}" # reencode to utf-8
dos2unix --quiet "${FILE_TEMP}" # transform line endings to LF
sed -i -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;ba' -e '}' "${FILE_TEMP}" # trim file (remove blank lines from begin and end)
sed -i '1s/^/\n/' "${FILE_TEMP}" # add one new line at the begining for compatibility with some devices
mv "${FILE_TEMP}" "${FILE_NEW}"