LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 09-30-2021, 10:38 AM   #1
petersfreeman
LQ Newbie
 
Registered: Sep 2021
Distribution: Ubuntu
Posts: 1

Rep: Reputation: Disabled
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
 
Old 10-01-2021, 11:32 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
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}"
 
1 members found this post helpful.
Old 10-01-2021, 02:42 PM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,605

Rep: Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548Reputation: 2548

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"

Last edited by boughtonp; 10-01-2021 at 02:44 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed , search exact and replace with a variable pedropt Programming 6 01-02-2018 01:03 PM
[SOLVED] search and replace particular word with sed using variable roopakl Linux - Newbie 2 10-01-2012 11:08 AM
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
converting Dos link backslash to Unix like backslash barunparichha Linux - Software 1 05-14-2009 09:54 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 06:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration