Sed-Skript (oder ein anderes Skript) zum Ersetzen eines Zeichens innerhalb der Erfassungsgruppe

Sed-Skript (oder ein anderes Skript) zum Ersetzen eines Zeichens innerhalb der Erfassungsgruppe

Ich versuche, Pandoc-Markup in Confluence-Wiki-Markup zu konvertieren. Ich verwendemarkdown2confluenceum den Großteil der Arbeit zu erledigen. Das funktioniert ganz gut, außer wenn ich über CSS und FreeMarker spreche, die {& }im Code verwenden, während Confluence {{& verwendet }}, um den Anfang/das Ende des Codeblocks zu markieren. Ich muss also ein in eingeschlossenes Muster abgleichen {{...}}.

Wenn ich (besser) Ruby kennen würde, könnte ich das Problem dort möglicherweise beheben, aber ich bin ein Unix-Typ der alten Schule, also dachte ich an awk oder sed.

So, ich bin soweit gekommen:

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

was braucht:

First we need a way to select a state (or group of states) CSS uses what
is called a selector to choose which elements to apply to, we have been
using one up until now without noticing, it is the {{*}} at the beginning
of our CSS. This is a special selector that means select everything. So
the rule that follows it (the bit between {{{}} and {{}}} apply to every
polygon on the map. But CSS allows us to insert a filter instead by
using {{[...]}} instead of {{*}}.

und produziert:

First we need a way to select a state (or group of states) CSS uses what
is called a selector to choose which elements to apply to, we have been
using one up until now without noticing, it is the {{"*"}} at the beginning
of our CSS. This is a special selector that means select everything. So
the rule that follows it (the bit between {{"{"}} and {{""}}} apply to every
polygon on the map. But CSS allows us to insert a filter instead by
using {{"[...]"}} instead of {{"*"}}.

Aber was ich brauche ist:

First we need a way to select a state (or group of states) CSS uses what
is called a selector to choose which elements to apply to, we have been
using one up until now without noticing, it is the {{*}} at the beginning
of our CSS. This is a special selector that means select everything. So
the rule that follows it (the bit between {{\{}} and {{\}}} apply to every
polygon on the map. But CSS allows us to insert a filter instead by
using {{[...]}} instead of {{*}}.

Außerdem muss damit umgegangen werden, {{${type.name}}}was werden soll {{$\{type.name\}}}.

Es gibt zwei Probleme

  1. Ich muss „ {durch“ ersetzen \{, anstatt Anführungszeichen zu verwenden, also muss ich es \1irgendwie ändern.
  2. Das hässliche Aussehen {{}}}(das „kommen“ heißen sollte) {{\}}}kommt nicht richtig raus, egal wie ich versuche, die Musterübereinstimmung zu beenden.

Antwort1

Der folgende Sed-Befehl scheint zu funktionieren:

   sed 's/{{\([^*[a-z][^}]*\)}}/{{\\\1}}/g;s/{{\\${\([^}]*\)}}}/{{$\\{\1\\}}}/g'

Erläuterung:

  1. {{\([^*[a-z][^}]*\)}}findet {{stuff}}, außer wenn mit oder oder einem Kleinbuchstaben stuffbeginnt .*[
  2. Ersetzen Sie es durch {{\stuff}}.
  3. Dann {{\\${\([^}]*\)}}}findet {{\${junk}}}.
  4. Und ersetzt es durch {{$\{junk\}}}.

Bearbeiten: Eine alternative Lösung könnte nach Klarstellung durch den OP folgende sein:

   sed 's/\({{[^}]*\){\([^}]*}}\)/\1\\{\2/g;s/\({{[^}]*\)}}}/\1\\}}}/g'

Wie wir alle wissen, kann sed kein rekursives Parsen durchführen, aber in den meisten einfachen Fällen sollte es funktionieren.

Erläuterung:

  1. \({{[^}]*\){\([^}]*}}\)findet {{foo{bar}}, wobei foound barnicht enthalten sind }.
  2. Und ersetzt es durch {{foo\{bar}}. (Hinweis: {{xxx{yyy}}}Wird ordnungsgemäß behandelt.)
  3. Dann \({{[^}]*\)}}}wird gefunden {{baz}}}, wo baznicht enthalten ist }.
  4. Und ersetzt es durch {{baz\}}}.

foo, bar, und bazkönnen leer sein, werden also beispielsweise bei Bedarf {{}}}in umgewandelt {{\}}}.

verwandte Informationen