Advertisement
j0h

chapterExtract

j0h
May 28th, 2023
1,122
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 1
  1. #!/bin/bash
  2. #set -xv
  3. #LaTeX text corpus  
  4. <<comment
  5. \documentclass[a4paper,12pt]{book}
  6. \usepackage[utf8]{inputenc}
  7. \usepackage{graphicx}
  8. \begin{document}
  9. \author{TeXstudio Team}
  10. \title{Simple Book Example}
  11. \date{January 2013}
  12. \frontmatter
  13. \maketitle
  14. \tableofcontents
  15. \chapter{Worste Latex doc ever}
  16. \chapter{I've wrote some bad data before}
  17. \chapter{but this is ... pretty bad}
  18. \chapter{Even for me.}
  19. \mainmatter
  20. \backmatter
  21. \end{document}
  22. comment
  23. # Input LaTeX file same as corpus comment above.
  24. latex_file="main.tex"
  25.  
  26. # Output directory for chapters
  27. output_dir="chapters"
  28.  
  29. # Create the output directory if it doesn't exist
  30. mkdir -p "$output_dir"
  31.  
  32. # Set the Internal Field Separator (IFS) to handle multi-line data
  33. IFS=''
  34.  
  35. #Chapter counter
  36.  i=0
  37.  
  38. # Read the LaTeX file
  39. while IFS= read -r line; do
  40.  
  41.   # Check if the line contains \chapter command
  42. if [[ $line == *"\chapter"* ]]; then
  43.     # Extract the chapter text within curly braces
  44.     chapter_text=$(echo "$line" | grep -oP '(?<=\{).*(?=\})')
  45.  
  46.     # Generate a chapter filename
  47.     chapter_filename=$(echo "$chapter_text" | tr -dc '[:alnum:][:space:]._-')
  48.  
  49.     # Save the chapter to the output directory
  50.     #echo "$chapter_text" > "$output_dir/$chapter_filename.txt"
  51.    
  52.     #Assign text to varables varNumber on the fly
  53.     chapterVar=$chapter_text$i
  54.    
  55.     declare "$chapterVar"="$chapter_text"
  56.     #Increment counter
  57.     i=$((i+1))
  58.  
  59. fi
  60. done < "$latex_file"
  61.  
  62. #<<comment
  63. # Print the chapter texts variables
  64. for (( c=0; c<=i; c++ ))
  65. do
  66.   chapterVar=$chapter_text$c
  67.   echo "Variable:  $chapterVar: ${!chapterVar}"
  68. done
  69. #comment
  70.  
  71. exit
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement