Sed (oder ein anderes) Skript, um einen Charakter in der Erfassungsgruppe zu ersetzen

1050
Ian Turton

Ich versuche, Pandoc-Markup in Confluence-Wiki-Markup zu konvertieren. Ich verwende markdown2confluence, um den Großteil der Arbeit zu erledigen. Dies funktioniert ziemlich gut, außer wenn ich über CSS und FreeMarker spreche, die {& }im Code verwenden, während Confluence {{& }}den Anfang / Ende des Codeblocks markiert. Ich muss also ein Muster in sich aufnehmen {{...}}.

Wenn ich Ruby kannte (mehr), könnte ich es vielleicht reparieren, aber ich bin ein alter Unix-Typ, also dachte ich an awk oder sed.

Also bin ich 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 {{"*"}}. 

Was ich aber 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 {{*}}. 

Muss auch damit umgehen, {{$}}was werden soll {{$\}}.

Es gibt zwei Probleme

  1. Ich brauche zu ersetzen {mit \{anstelle der Verwendung von Anführungszeichen, also muss ich ändern \1irgendwie.
  2. Das fiese Aussehen {{}}}(was kommen sollte) {{\}}}kommt nicht richtig heraus, egal wie ich versuche, das Muster zu beenden.
0
Können Sie die gewünschte Ausgabe des obigen Beispiels posten? chaos vor 8 Jahren 1
Wenn Sie sed -r verwenden, wäre es etwas weniger hässlich. Dann könnten Sie sagen (`` anstelle von `\ (` barlop vor 8 Jahren 0

1 Antwort auf die Frage

2
Joseph Quinsey

The following sed command seems to work:

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

Explanation:

  1. {{\([^*[a-z][^}]*\)}} finds {}, except when stuff begins with * or [ or a lower-case letter.
  2. Replace it with {{\stuff}}.
  3. Then {{\\${\([^}]*\)}}} finds {{\$}}.
  4. And replaces it with {{$\}}.

Edit: An alternative solution, after clarification from the OP, could be this:

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

As we all know, sed cannot do recursive parsing, but this should work for most simple cases.

Explanation:

  1. \({{[^}]*\){\([^}]*}}\) finds {}, where foo and bar do not contain }.
  2. And replaces it with {}. (Note {}} is handled ok.)
  3. Then \({{[^}]*\)}}} finds {}}, where baz does not contain }.
  4. And replaces it with {}}.

foo, bar, and baz can be empty, so for example {{}}} is converted to {{\}}}, as required.

close aber es wandelt {} in {{\ type}} um, während es {} bleiben soll Ian Turton vor 8 Jahren 0
@iant Ich habe `az` hinzugefügt, um sicherzustellen, dass` {} `unverändert bleibt. Aber wie lauten die Regeln für "Typ"? Joseph Quinsey vor 8 Jahren 0
Grundsätzlich sollte alles, was kein ist, zwischen dem {{}} unverändert bleiben. Ian Turton vor 8 Jahren 0
Danke für die zusätzlichen Infos. Vielleicht lösche ich diese Antwort und schicke eine bessere Antwort. Joseph Quinsey vor 8 Jahren 0
Sie schreiben: "Vielleicht lösche ich diese Antwort und übermittle eine bessere." <--- Es gibt eine Schaltfläche zum Bearbeiten. barlop vor 8 Jahren 2