sed multiline Text-Ersetzungfrage
6 Antworten
| Nachricht | Autor |
|---|---|
|
Donnerstag, 21. Dezember 2006 12:16:48
Hallo Ich versuche einen großen Text mittels SED One-Liner'n in ein anderes Aussehen zu konvertieren. E: Gab es bla bla bla schon lange ?
kkk: Nein nein nein. Da war das ein dicker Hund!
E: Ein Hund - mit Fell ?!
kkk: Ja ja.
Ein dicker Hund mit hellem Fell.
E: Was hat der gemacht?
Hatte der braune Augen ?
Oder was ?
kkk: Weiss ich nicht!
<strong>Mein noch nicht ganz perfekter SED onliner:</strong>
more test_text.txt | sed 's/E: \(.*\)$/eee: <strong>/'
<strong>Das nicht ganz perfekte Ergebnis:</strong>
eee: <strong>Gab es bla bla bla schon lange ?</strong>
kkk: Nein nein nein. Da war das ein dicker Hund!
eee: <strong>Ein Hund - mit Fell ?!</strong>
kkk: Ja ja.
Ein dicker Hund mit hellem Fell.
eee: <strong>Was hat der gemacht?</strong>
Hatte der braune Augen ?
Oder was ?
kkk: Weiss ich nicht!
<strong> Das gewünschtes Ergebnis:</strong>
eee: <strong>Gab es bla bla bla schon lange ?</strong>
kkk: Nein nein nein. Da war das ein dicker Hund!
eee: <strong>Ein Hund - mit Fell ?!</strong>
kkk: Ja ja.
Ein dicker Hund mit hellem Fell.
eee: <strong>Was hat der gemacht?
Hatte der braune Augen ?
Oder was ?<strong></strong></strong>
kkk: Weiss ich nicht!
|
Enno Bartels
|
| Antworten | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Anbei bin ich per Zufall auf eine mögliche Lösung der Aufgabe gestoßen. Eine Abhängigkeit geht mit dem "N" Kommando (dieses Kommando erweitert die Möglichkeiten um eine Abhängigkeit über eine Zeile hinaus zu bekommen). Unten habe ich die Quelle als Link angegeben, und falls diese nicht mehr exestiert, die betreffenden Stellen hier hinein kopiert. The N command no longer discards the contents of the pattern space upon reaching the end of file. This is not a bug, it's a feature. However, it breaks certain scripts which relied on the older behavior of N. 'N' adds the Next line to the pattern space, enabling multiple lines to be stored and acted upon. Upon reaching the last line of the file, if the N command was issued again, the contents of the pattern space would be silently deleted and the script would abort (this has been the traditional behavior). For this reason, sed users generally wrote: $!N; # to add the Next line to every line but the last one. However, certain sed scripts relied on this behavior, such as the script to delete trailing blank lines at the end of a file (see script #12 in section 3.2, "Common one-line sed scripts", above). Also, classic textbooks such as Dale Dougherty and Arnold Robbins' sed & awk documented the older behavior. The GNU sed maintainer felt that despite the portability problems this would cause, changing the N command to print (rather than delete) the pattern space was more consistent with one's intuitions about how a command to "append the Next line" ought to behave. Another fact favoring the change was that "{N;command;}" will delete the last line if the file has an odd number of lines, but print the last line if the file has an even number of lines. To convert scripts which used the former behavior of N (deleting the pattern space upon reaching the EOF) to scripts compatible with all versions of sed, change a lone "N;" to "$d;N;". # GNU style sed script $!N; # except for last line, get next line s/^\([0-9]\{5\}\).*\n\1.*//; # if first 5 digits of each line # match, delete BOTH lines. t skip P; # print 1st line only if no match :skip D; # delete 1st line of pattern space and loop #---end of script--- [1] http://www.student.northpark.edu/pemente/sed/sedfaq6.html | ||||||||||||||||||||||||||||||||
|



