Multiline parenthesis matching, with e.g. SED script, in LaTeX doc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiline parenthesis matching, with e.g. SED script, in LaTeX doc
# 1  
Old 09-29-2010
Question Multiline parenthesis matching, with e.g. SED script, in LaTeX doc

In a LaTeX manuscript, I need to replace many occurrences of

\emph{some string}

with some string, i.e. whatever string is inside. The string inside often may extend over several lines, and there may be other occurences of curly brackets inside it. So for example

Code:
\emph{this \it{is} a
test} 

should be this

this \it{is} a
test

Any good ideas?

Sune

Last edited by sune; 09-29-2010 at 03:38 PM.. Reason: add code tag
# 2  
Old 09-29-2010
so from this sample,

Code:
\emph{this \it{is} a
test}

what's your expect output?
# 3  
Old 09-29-2010
So basically...

[1]
So basically you're saying that given

Code:
\emph{ alpha }

where alpha is any arbitrary string of characters (including newlines), you wish to replace it with

Code:
alpha

correct?

[2]
Do you know if the \emph tags are nested? If they are nested then that would require a pushdown-automata, as regular expressions alone (i.e., finite automatons) are insufficient to parse balanced-items (such as balanced parenthetical statements, for example).
# 4  
Old 09-30-2010
rdcwayx: That would be

Code:
this \it{is} a
test



---------- Post updated at 12:13 AM ---------- Previous update was at 12:11 AM ----------

Hi Mubby,
Sorry didn't see your second question. So for completeness:
1) Yes, you got the idea.

2) No, there are no nested occurences of emph! (But there may be curly brackets in alpha).
Sune

---------- Post updated at 03:06 AM ---------- Previous update was at 12:13 AM ----------

Found a piece of (recursive) code and have modified it:
Code:
:top
/\\emph{.*}/ {
s/\\emph{\([^{}]*\)}/\1/g
t top
}
/{/{
        N
        b top
        }

Doesn't work however, but I have a feeling it just needs a tiny modification .....??
# 5  
Old 09-30-2010
Assuming all the curly braces are balanced:
Code:
perl -0ne 's/\\emph{//g;$i=0;while(/./gs){$i-- if $& eq "{";$i++ if $& eq "}"; if ($i<1){print $&}else{$i=0}}' infile > outfile

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 09-30-2010
Worked like a charm!
Thanks!
Sune
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed multiline problem

I'm trying to replicate the sed output on p.108 of Sed&Awk,by Doughery & Robbins, 2nd edition. I'm on a Windows 10 Surface Pro, running Cygwin for 64-bit versions of Windows. Input text saved in text file called data_p108.txt: Consult Section 3.1 in the Owner and Operator Guide for a... (9 Replies)
Discussion started by: prooney
9 Replies

2. Shell Programming and Scripting

Multiline sed

Hi guys, I am fairly comfortable with using the sed command if the string to be replaced is all on a single line. I was wondering is it possible to use sed command in a multiline way ? Say for example I have the below string on 2 different lines: { "key": "brandNameA", ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

sed help adding parenthesis

I have the following data and want to put parenthis around the numbers: PARTITION PERIOD_MIN VALUES LESS THAN 10649 TABLESPACE ODS_DAILY_MF_AUM, PARTITION PERIOD_10649 VALUES LESS THAN 10650 TABLESPACE ODS_DAILY_MF_AUM, PARTITION PERIOD_10650 VALUES LESS THAN 10651 TABLESPACE... (2 Replies)
Discussion started by: BeefStu
2 Replies

4. UNIX for Dummies Questions & Answers

Need Multiline sed help!!

Hey everyone, I'm new to sed and I need to create a script for inserting one line of code at the beginning of every method in a Xcode project (over 6,000 methods). Each method Structure is (+ or -) (Various declarations-- could span multiple lines) ({) I've tried for days, any guidance would be... (2 Replies)
Discussion started by: jimmyz
2 Replies

5. Shell Programming and Scripting

Need help with Sed (replacing parenthesis and comma)

I have the following text as an input text: input.txt Results('Toilet', 'Sink', ) and i want to remove the last comma so the output is output.txt Results('Toilet', 'Sink' ) I tried using the following sed command, but I get a parsing error: sed s/, \)/\)/g input.txt >... (5 Replies)
Discussion started by: jl487
5 Replies

6. Shell Programming and Scripting

awk multiline matching

I have a file that looks something like this with lots of text before and after. Distance method: Sum of squared size difference (RST) </data> <pairwiseDifferenceMatrix time="02/08/11 at 13:08:27"> 1 2 1 448.82151 507.94231 2 ... (7 Replies)
Discussion started by: mgray
7 Replies

7. Shell Programming and Scripting

multiline pattern matching

Hi, I have a file of the following from: Afghanistan gdpcapit|800 Akrotiri Albania gdpcapit|6000 now I want have the gdpcapit value next to the country when there is one like this: Afghanistan 800 gdpcapit|800 Akrotiri Albania 6000 gdpcapit|6000 How do I do this? I've... (4 Replies)
Discussion started by: KarelVH
4 Replies

8. Shell Programming and Scripting

Preparing LaTeX files using Sed/AWK for processing with latex2html

As the title states, my issue involves preparing LaTeX documents for processing with latex2html. Within my LaTeX docs I have used a package which allows me to cleanly display source code listings. However the package is not supported by latex2html which, when processed, does not display the... (3 Replies)
Discussion started by: oski
3 Replies

9. UNIX for Dummies Questions & Answers

another sed question about parenthesis

hi, I'm trying to use sed to erase everything, up to, and including, the first closing parenthesis. for example: input: blah blah blah (aldj) test (dafs) test test. output: test (dafs) test test. how would i do this? I was fooling around with the parenthesis, and i only got it to apply to... (5 Replies)
Discussion started by: gammaman
5 Replies
Login or Register to Ask a Question