Village
Village

Reputation: 24433

Replacing quotation marks with \quotation and \quote

I have a document containing many quotation marks. I need to replace all pairs of " with \quotation{ (beginning) and } (ending), for use in ConTeXt, e.g.:

"Do not be afraid," said the tiger, "I am a vegetarian."

This should become:

\quotation{Do not be afraid,} said the tiger, \quotation{I am a vegetarian.}

How can I replace these quotation marks with the format used by ConTeXt?

Upvotes: 2

Views: 471

Answers (5)

potong
potong

Reputation: 58488

This might work for you:

echo -e 'a "b" c "d" e\na "b" c "d e\na "b" c "d/d" e' |
sed 'h;s/"\([^"/]*\)"/\\quotation{\1}/g;/"/{g;s/^/ERROR: /}'
a \quotation{b} c \quotation{d} e
ERROR: a "b" c "d e
ERROR: a "b" c "d/d" e

if you don't want the ERROR message, then:

echo -e 'a "b" c "d" e\na "b" c "d e\na "b" c "d/d" e' | 
sed 'h;s/"\([^"/]*\)"/\\quotation{\1}/g;/"/g'
a \quotation{b} c \quotation{d} e
a "b" c "d e
a "b" c "d/d" e

Upvotes: 1

pizza
pizza

Reputation: 7640

Another way:

perl -n -e '$a=$_;$a=~s/\"([^\"^\\]*)\"/\\quotation\{$1\}/g;print $a' < input

Upvotes: 2

Alexander Putilin
Alexander Putilin

Reputation: 2342

Here is my awk code, which is maybe not very elegant, but it does the job.

{
    # split current line into several pieces using quotation char
    split($0, a, "\"")
    # and if the number of pieces is even, which implies that the number of quotation marks is odd
    if (length(a) % 2 == 0) {
        # Then error, unclosed quotation mark
        # Handle it in some other way if you want
        print
    } else {
        # the only pieces that need to be quoted are those on even positions in array
        # so we just surround them with the desired text
        for (i = 2; i <= length(a); ++i) {
            if (i % 2 == 0) {
                printf "%s", "\\quote{" a[i]
            } else {
                printf "%s", "}" a[i]
            }
        }
        # We should output end-of-line character manually to end the line
        printf "\n"
    }
}

It works by splitting the line into parts using quotation characte and stores them in a array, so for example line, "Do not be afraid," said the tiger, "I am a vegetarian.":

a[1]: 
a[2]: Do not be afraid,
a[3]:  said the tiger, 
a[4]: I am a vegetarian.
a[5]: 

a[1] and a[5] are both empty

Upvotes: 2

jaypal singh
jaypal singh

Reputation: 77155

Not perfect, but you can try something like this -

sed 's/"\(.[^"]*\)"/\\quotation{\1}/g' file

Test:

[jaypal:~/Temp] cat file
"Do not be afraid," said the tiger, "I am a vegetarian."

[jaypal:~/Temp] sed 's/"\(.[^"]*\)"/\\quotation{\1}/g' file
\quotation{Do not be afraid,} said the tiger, \quotation{I am a vegetarian.}

Upvotes: 2

sarnold
sarnold

Reputation: 104080

This sounds like a horrible thing to automate; the complexity can be impressive:

She said, "Don't say 'stupid', or I'll smack you.", to John's girlfriend.

There's no good way to tell the difference between an embedded quote, contractions, possessive quotes, and the nesting could be horrible to match. A forgotten closing quote somewhere could completely screw up the output. (I've seen dozens of missing quotes in e.g. Terry Pratchett books. Is your content in better shape?)

Upvotes: 2

Related Questions