From 3410957620b306b599446bfe77c935e612c33a92 Mon Sep 17 00:00:00 2001 From: David Lecompte <3gpp@metani.eu> Date: Fri, 24 Jul 2026 14:31:03 +0200 Subject: [PATCH] Adding docx2txt to convert a docx to a txt file --- bin/docx2txt.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 bin/docx2txt.sh diff --git a/bin/docx2txt.sh b/bin/docx2txt.sh new file mode 100755 index 0000000..8bf5a3c --- /dev/null +++ b/bin/docx2txt.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +if [[ $# -eq 0 ]] ; then + echo "Usage: ${0##*/} file [files]" + echo "Extract ASN.1 from one or more 3GPP 38.331 docx files and save them to .txt files" + exit 1 +fi + +if [[ ! -w . ]] ; then + echo "Cannot write to current directory" + exit 1 +fi + +platform=$(uname) + +case $platform in + Linux) libreoffice="libreoffice" ;; + CYGWIN*) libreoffice="soffice" ;; + *) echo "Unkown platform $platform" + exit 1;; +esac + +if [[ ! -x $(which "$libreoffice") ]] ; then + echo "Cannot find libreoffice executable" + echo "Make sure it is installed and the PATH variable includes its path" + exit 1 +fi + +while [ "$#" -gt 0 ]; do + + echo "Processing $1" + + if [[ ! -f "$1" ]] || [[ ! -r "$1" ]] ; then + echo "$1 is not a readable file" + shift + continue + fi + + $libreoffice --convert-to txt "$1" + + if [[ ! -f "${1%.docx}.txt" ]] || [[ ! -r "${1%.docx}.txt" ]] ; then + echo "libreoffice did not create ${1%.docx}.txt" + shift + continue + fi + + case $platform in + # Replace EN-DASH (UTF-8 encoding) at line start with - + Linux) LC_ALL=C sed -i $'s/^\u2013/-/g' "${1%.docx}.txt" ;; + + # Replace EN-DASH (Windows-1252 encoding) at line start with - + # Remove CR (0x0d) put by libreoffice in windows before each LF (0x0a) + CYGWIN*) LC_ALL=C sed -i $'s/^\x96/-/g; s/\r$//' "${1%.docx}.txt" ;; + *) echo "Unkown platform $platform" + exit 1;; + esac + + # Changes things that should not be so in the specification + # Change "-- Cond N3C MP" to "-- Cond N3C-MP" (to respect condition naming rules) + LC_ALL=C sed -i 's/-- Cond N3C MP/-- Cond N3C-MP/' "${1%.docx}.txt" + LC_ALL=C sed -i 's/^N3C MP/N3C-MP/' "${1%.docx}.txt" + LC_ALL=C sed -i 's/-- Cond HOAndServCellAdd,$/-- Cond HOAndServCellAdd/' "${1%.docx}.txt" + shift +done + -- 2.47.3