LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Search and replace backslash variable combination using sed works with CLI but not script (https://www.linuxquestions.org/questions/linux-desktop-74/search-and-replace-backslash-variable-combination-using-sed-works-with-cli-but-not-script-4175701285/)

petersfreeman 09-30-2021 10:38 AM

Search and replace backslash variable combination using sed works with CLI but not script
 
I'm using Pandoc to convert a LaTeX file of a play to markdown. Pandoc unfortunately does not treat the specialized backslash commands used by the Dramatist package properly, eliminating all of the dialogue.

To get around this problem, I wrote a bash script to pre-process the LaTeX text file.

I'm having a peculiar problem with the use of sed to change a backslash-charactername combination to an upper-case version of the charactername. For example, I want this line:

\direct{\Benjamin goes to the door and lets \Noah in}

to be converted to:

\direct{BENJAMIN goes to the door and lets NOAH in}

As I am producing a book of twelve plays, there are over a hundred character names all told. My bash script reads the character names from a file and uses variable substitution to change each character name from its backslash proper case version to a non-backslashed upper-case version. When I test the sed line in a CLI environment, it works; when I test it in the script it doesn't.

If I type these lines in a CLI environment it works:

Code:

Input_File="Test.tex"
Character="Benjamin"
Upper_Case_Character="${Character^^}"
cat "${Input_File}" | sed "s/\\\\${Character}/${Upper_Case_Character}/g"

If I write a script with this code, it doesn't work.

Code:

Input_File="Test.tex"
while IFS="" read Character; do
  Upper_Case_Character="${Character^^}"
  cat "${Input_File}" | sed "s/\\\\${Character}/${Upper_Case_Character}/g"
done < "${Char_File}"

I've tried increasing the number of backslashes to no avail.

Here is the contents of the file Test.tex:

Code:

\documentclass{article}

\usepackage[lnps]{dramatist}

\title{Sample Play}
\author{Peter Freeman}
\date{\today}

\begin{document}
       
        \maketitle

        \Character[\Noah{} -- Noah is a gangling teenager.]{Noah}{Noah}
        \Character[\Benjamin{} -- Benjamin is a solidly built teen.]{Benjamin}{Benjamin}
       
        \DramPer
       
       
        \act
       
        \scene
       
        \StageDir{\Benjamin's bedroom is part of a recreational basement.}
       
        \begin{drama}
                \speaker{Noah} Whatcha doin', Benj?
               
                \direct{\Noah walks into \Benjamin's basement bedroom.}

                \direct{\Benjamin looks up over the smudged pair of glasses he always wears when reading.}
               
                \speaker{Benjamin} I'm figuring out how to build a time machine.
               
                \direct{\Noah throws himself down on an old bean bag that sits against the wall opposite \Benjamin's desk.}
               
                \speaker{Benjamin} Yeah\ldots{}but that's sus. They don't really figure it out properly. There's rules.
               
                \direct{\Benjamin uses his pencil to emphasize his point.}
               
                \speaker{Noah} Sure. You gotta make sure you don't change things, right?
        \end{drama}
       
\end{document}

Thank you,

Peter

pan64 10-01-2021 11:32 AM

yes, because the problem is not the sed, but the handling of stdin.
I guess you can try something like this:
Code:

Input_File="Test.tex"
while IFS="" read Character; do
  Upper_Case_Character="${Character^^}"
  sed "s/\\\\${Character}/${Upper_Case_Character}/g" "${Input_File}" </dev/null
done < "${Char_File}"


boughtonp 10-01-2021 02:42 PM


 
You don't need to do the uppercasing with Bash - Sed can do it with \U:
Code:

#!/bin/sh

Input_File=Test.tex
Output_File=Test.output

cp "$Input_File" "$Output_File"

Characters="Benjamin
Noah"

while IFS= read -r Character; do
  sed -i 's/\\\('"$Character"'\)/\U\1/g' "$Output_File"
done <<< "$Characters"

cat "$Output_File"



All times are GMT -5. The time now is 08:17 AM.