Вот мой awk
код, который может быть не очень элегантным, но он выполняет свою работу.
{
# 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"
}
}
Он работает, разбивая строку на части, используя кавычки, и сохраняет их в массиве a
, например, строка «Не бойся», сказал тигр: «Я вегетарианец»:
a[1]:
a[2]: Do not be afraid,
a[3]: said the tiger,
a[4]: I am a vegetarian.
a[5]:
a [1] и a [5] оба пусты