Что ж, у меня есть ОЧЕНЬ уродливое решение ... Он обрабатывает все, кроме черточек в строках.
Надеюсь, твои глаза не пострадают ...
fg@erwin ~ $ perl -ne 'my @l; foreach (split /-/) { my ($start, $middle, $end) = m/^([^"]+)"(.*)"([^"]*)$/ or do { push @l, $_; next; }; $middle =~ s/"/\\"/g;push @l, "$start\"$middle\"$end"; } END { print join("-", @l) . "\n";}' <<EOF
> command -text "hello this is "some text" with "quotes inside"" -other "another thing""" -another " -another "text"
> EOF
command -text "hello this is \"some text\" with \"quotes inside\"" -other "another thing\"\"" -another " -another "text"
Объясняя это немного ...
my @l; # declare a new list l, which will contain the "transformed" strings
foreach (split /-/) { # split against the dash -- no loop variable: $_ has the content
# Try and capture in start, middle and end:
# * start: from the beginning until the first quote;
# * middle: everything but the last quote and whatever is after it;
# * end: what is after the last quote
# By default, m// applies to $_
my ($start, $middle, $end) = m/^([^"]+)"(.*)"([^"]*)$/
or do { # if no match...
push @l, $_; # put the line as is,
next; # and continue
};
$middle =~ s/"/\\"/g; # in the middle part, precede all quotes with a \
push @l, "$start\"$middle\"$end"; # and push the final string onto the list
}
# finally, print all transformed lines joined with a dash, plus a newline
END { print join("-", @l) . "\n";}