Match zwei Reihen

Match zwei Reihen

am 07.03.2006 22:38:26 von Marek Stepanek

Hallo an alle,


ich habe Schwierigkeiten, zwei Zeilen mit meinem Skript zu "matschen" (wie
heißt das auf Deutsch?).

Was ich möchte ist ein Skript, das einen gegebenen Text formatiert wie
folgt:

****original****
here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"

****formatiert******
here it is: there comes the rest of it
one colon more: and what goes with it
short: this is an example of a short ":"

Später möchte ich lange Doppelpunkte folgendermaßen formatieren - aber das
ist hier nicht die Frage. (Das nur als Warnung, dass dies als Frage kommen
wird, wenn ich das nicht alleine schaffe :)

****formatiert******
here it is: there comes the rest of it
one colon more: and what goes with it
short: this is an example of a short ":"
and a very long colon:
and here the rest of the very long colon!


mein Skript, das ich mir als Anfänger ausgeknobelt habe, funktioniert nicht.
Und ich verstehe nicht, wieso:

Kann mir jemand einen Tipp geben?


Danke! marek


#!/usr/bin/perl -w

use strict;

$/='';

my ($line, $vor_doppelpunkt, $nach_doppelpunkt);

my (@lines);

while ()
{
push @lines, "$1\t\t\t$2" if
(/^\s*([^:]+?:)[[:blank:]]*\n[[:blank:]]*(.+)$/sm);
# push @lines, "$1\t\t\t$2" if (/^\s*([^:]+?:)\s*\n\s*(.+)$/sm);
}

print "\n";

foreach $line (@lines) {
print "$line\n";
}

print "\n";

__DATA__

here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"

Re: Match zwei Reihen

am 07.03.2006 23:05:58 von Dominik Seelow

Marek Stepanek wrote:
>
>
> Hallo an alle,
>
>
> ich habe Schwierigkeiten, zwei Zeilen mit meinem Skript zu "matschen" (wie
> heißt das auf Deutsch?).
>
> Was ich möchte ist ein Skript, das einen gegebenen Text formatiert wie
> folgt:
>
> ****original****
> here it is:
> there comes the rest of it
> one colon more:
> and what goes with it
> short:
> this is an example of a short ":"
>
> ****formatiert******
> here it is: there comes the rest of it
> one colon more: and what goes with it
> short: this is an example of a short ":"

Ein Schnellschuß:

use strict;
while (){
chomp;
s/^\s+//;
/(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;
}
__DATA__

here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"


HTH,
Dominik

Re: Match zwei Reihen

am 08.03.2006 05:10:36 von Marek Stepanek

On 07.03.2006 23:05, in article 440E03C6.9060109@yahoo.co.uk, "Dominik
Seelow" wrote:

> Ein Schnellschuß:
>
> use strict;
> while (){
> chomp;
> s/^\s+//;
> /(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;
> }
> __DATA__
>
> here it is:
> there comes the rest of it
> one colon more:
> and what goes with it
> short:
> this is an example of a short ":"
>
>
> HTH,
> Dominik


Hallo Dominik,


Deinen Zaubertick verstehe ich nicht. Jedenfalls Danke. Das Ergebnis sieht
in meiner Shell folgendermaßen aus:

[markslap:BBEdit/Unix Support/Unix Filters] mareklap%
../format_doublepoints.pl
here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"[markslap:BBEdit/Unix Support/Unix Filters]
mareklap%

wobei der Zeilensprung in der ersten und letzten Zeile von meinem E-Mail
Programm eingefügt sind. Habe ich etwas falsch verstanden?


marek

Re: Match zwei Reihen

am 08.03.2006 10:47:21 von Christian Kirsch

Marek Stepanek schrieb:
>
>
> Hallo an alle,
>
>
> ich habe Schwierigkeiten, zwei Zeilen mit meinem Skript zu "matschen" (wie
> heißt das auf Deutsch?).

*So* sicherlich nicht. Es sei denn, es geht um nasse Erde, vulgo
Matsch. Ich würde mal "finden" vorschlagen.

Re: Match zwei Reihen

am 08.03.2006 12:03:11 von Dominik Seelow

Marek Stepanek wrote:
> On 07.03.2006 23:05, in article 440E03C6.9060109@yahoo.co.uk, "Dominik
> Seelow" wrote:

Hallo Marek,

> Deinen Zaubertick verstehe ich nicht. Jedenfalls Danke. Das Ergebnis sieht
> in meiner Shell folgendermaßen aus:

ich erklär das dann mal im Code...

>> s/^\s+//;
Leerzeichen/Tabs am Anfang entfernen

>> /(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;

endet die Zeile mit einem Doppelpunkt (und evtl. folgenden
Leerzeichen/Tabs)?
ja: den 'eingefangenen' Text vor dem Doppelpunkt mitsamt einem neuen
Doppelpunkt linksbündig ausgeben (auf 30 Zeichen)
nein: die Zeile ausdrucken, davor ein Tab setzen (falls mehrere Zeilen
ohne Doppelpunkt folgen, werden diese Tab-getrennt dargestellt)

> ./format_doublepoints.pl
> here it is:
> there comes the rest of it
> one colon more:
> and what goes with it
> short:
> this is an example of a short ":"[markslap:BBEdit/Unix Support/Unix Filters]
> mareklap%

Naja, da fehlt einfach eine weitere Newline am Ende des Scripts. :-)

> wobei der Zeilensprung in der ersten und letzten Zeile von meinem E-Mail
> Programm eingefügt sind. Habe ich etwas falsch verstanden?

Hmm, also bei mir sieht das so aus:

here it is: there comes the rest of it
one colon more: and what goes with it
short: this is an example of a short ":"

Du kannst ja mal

/(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;

durch

/(.*):\s*$/?print sprintf ("\n%-30s",uc $1.':'):print "\t".$_;

ersetzen, dann siehst Du, ob der 'Zeilenname' gefunden wurde. Es sieht
ja in Deinem Output in der ersten Zeile nicht danach aus.


Viele Grüße,
Dominik

Re: Match zwei Reihen

am 08.03.2006 15:55:30 von David Haller

Dominik Seelow wrote:
> /(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;
^^^^^^^^^^^^^

Wieso das eigentlich? Warum nicht einfach 'printf'?

-dnh

--
God must love the Common Man; He made so many of them.
-- BSD fortune file

Re: Match zwei Reihen

am 08.03.2006 17:05:23 von Christian Winter

Marek Stepanek schrieb:
> ich habe Schwierigkeiten, zwei Zeilen mit meinem Skript zu "matschen" (wie
> heißt das auf Deutsch?).

Das kannst Du ruhig eindeutschen, aber lass das "s" raus.

> Was ich möchte ist ein Skript, das einen gegebenen Text formatiert wie
> folgt:
>
> ****original****
> here it is:
> there comes the rest of it
> one colon more:
> and what goes with it
> short:
> this is an example of a short ":"
>
> ****formatiert******
> here it is: there comes the rest of it
> one colon more: and what goes with it
> short: this is an example of a short ":"
>
[...]
>
> mein Skript, das ich mir als Anfänger ausgeknobelt habe, funktioniert nicht.
> Und ich verstehe nicht, wieso:
>
> Kann mir jemand einen Tipp geben?

Aus der Hüfte:
------------------------------------------------------------
#!/usr/bin/perl

use strict;
use warnings;

local $/='';

$_ = ;

s|
^\s* # Leerzeichen am Zeilenanfang wegwerfen
([^:]+:) # Dann alles inkl. Doppelpunkt capturen
\s* # Leerzeichen nach dem Doppelpunkt weg
\r?\n # Zeilensprung entferen
\s* # Und wieder Leerzeichen am Zeilenanfang
|
$1 .
"\t" x (4 - int(length($1)/8)) # 24 Zeichen von links
|emgx;

print;

__DATA__
here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"
------------------------------------------------------------

HTH
-Christian

Re: Match zwei Reihen

am 08.03.2006 22:02:18 von Dominik Seelow

David Haller wrote:
> Dominik Seelow wrote:
>> /(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;
> ^^^^^^^^^^^^^
>
> Wieso das eigentlich? Warum nicht einfach 'printf'?

Weil da vorher

print /(.*):\s*$/?sprintf ("\n%-30s",$1.':'):"\t".$_;

stand, was ich dann aber so verwirrend fand, daß ich das print lieber
vor die beiden Möglichkeiten gesetzt habe - ohne dabei daran zu denken
das 'print sprintf' zu 'printf' zusammenzufassen. :-)

Re: Match zwei Reihen

am 08.03.2006 23:51:17 von Marek Stepanek

On 08.03.2006 22:02, in article 440F465A.9090905@yahoo.co.uk, "Dominik
Seelow" wrote:

> David Haller wrote:
>> Dominik Seelow wrote:
>>> /(.*):\s*$/?print sprintf ("\n%-30s",$1.':'):print "\t".$_;
>> ^^^^^^^^^^^^^
>>
>> Wieso das eigentlich? Warum nicht einfach 'printf'?
>
> Weil da vorher
>
> print /(.*):\s*$/?sprintf ("\n%-30s",$1.':'):"\t".$_;
>
> stand, was ich dann aber so verwirrend fand, daß ich das print lieber
> vor die beiden Möglichkeiten gesetzt habe - ohne dabei daran zu denken
> das 'print sprintf' zu 'printf' zusammenzufassen. :-)



Vielen Dank für die vielen Anregungen. Leider kann ich nur zu extremen
Zeiten antworten. Also sehr spät oder sehr früh ...

Ich habe heute lange über Dominiks Vorschlag gebrütet und habe schließlich
verstanden, wie das funktioniert. Heute (extrem früh:) hatte ich vergessen,
in meinem Skript das

$/ = '';

zu entfernen, was natürlich diesen Vorschlag unbrauchbar gemacht hat. Dann
habe ich mir folgende Änderungen einfallen lassen:

***Original***

here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"
and here comes a very long double point, in English colon:
the rest which goes with it!

***Formatiert***

here it is: there comes the rest of it
one colon more: and what goes with it
short: this is an example of a short ":"
and here comes a very long double point, in English colon:
the rest which goes with it!

ich dachte mir, dass dies mit dem Quantifier {min,} bzw {,max} zu
bewerkstelligen wäre ... Funktioniert aber leider nicht so!

nochmal Danke an alle, und auch Danke für die Geduld! - für den Vorschlag
von Christian brauche ich noch mal einen Tag um ihn zu verstehen :-)

#!/usr/bin/perl -w

use strict;

#$/='';


while (){
chomp;
s/^\s+//;
/(.{,10}):\s*$/?printf ("\n%-10s",$1.':'):print "\t\t".$_;
/(.{10,}):\s*$/?printf ("\n%-10s",$1.':'):print "\t\t".$_;

}

print "\n\n";

__DATA__

here it is:
there comes the rest of it
one colon more:
and what goes with it
short:
this is an example of a short ":"
and here comes a very long double point, in English colon:
the rest which goes with it!

Re: Match zwei Reihen

am 09.03.2006 01:08:25 von Dominik Seelow

Marek Stepanek wrote:

> Vielen Dank für die vielen Anregungen. Leider kann ich nur zu extremen
> Zeiten antworten. Also sehr spät oder sehr früh ...
>
> Ich habe heute lange über Dominiks Vorschlag gebrütet und habe schließlich
> verstanden, wie das funktioniert. Heute (extrem früh:) hatte ich vergessen,
> in meinem Skript das
>
> $/ = '';
>
> zu entfernen, was natürlich diesen Vorschlag unbrauchbar gemacht hat. Dann
> habe ich mir folgende Änderungen einfallen lassen:
>
> ***Original***
>
> here it is:
> there comes the rest of it
> one colon more:
> and what goes with it
> short:
> this is an example of a short ":"
> and here comes a very long double point, in English colon:
> the rest which goes with it!
>
> ***Formatiert***
>
> here it is: there comes the rest of it
> one colon more: and what goes with it
> short: this is an example of a short ":"
> and here comes a very long double point, in English colon:
> the rest which goes with it!
>
> ich dachte mir, dass dies mit dem Quantifier {min,} bzw {,max} zu
> bewerkstelligen wäre ... Funktioniert aber leider nicht so!
>
> nochmal Danke an alle, und auch Danke für die Geduld! - für den Vorschlag
> von Christian brauche ich noch mal einen Tag um ihn zu verstehen :-)
>
> #!/usr/bin/perl -w
>
> use strict;
>
> #$/='';
>
>
> while (){
> chomp;
> s/^\s+//;
> /(.{,10}):\s*$/?printf ("\n%-10s",$1.':'):print "\t\t".$_;
> /(.{10,}):\s*$/?printf ("\n%-10s",$1.':'):print "\t\t".$_;

Na, hier würdest Du ja alles zweimal drucken... :-) Außerdem willst Du
ja bei langen Beschreibern einen Zeilenumbruch einfügen (willst Du
doch?) und brauchst danach wieder entsprechend viele Leerzeichen.

while (){
chomp;
s/^\s+//;
if (/(.*):\s*$/){
printf "\n%-15s",$1.':';
printf "\n%-15s",'' if length $1>14
# die 15. Position ist ja der Doppelpunkt!
}
else {
print "\t".$_;
}
}


Viele Grüße,
Dominik

Re: Match zwei Reihen

am 09.03.2006 07:24:44 von Marek Stepanek

On 09.03.2006 01:08, in article 440F71F9.8040405@yahoo.co.uk, "Dominik
Seelow" wrote:

> while (){
> chomp;
> s/^\s+//;
> if (/(.*):\s*$/){
> printf "\n%-15s",$1.':';
> printf "\n%-15s",'' if length $1>14
> # die 15. Position ist ja der Doppelpunkt!
> }
> else {
> print "\t".$_;
> }
> }


Wow! da habe ich einiges zu verdauen bis heute Abend! Es funktioniert und
ich verstehe nicht warum ...


Danke noch mal an alle


marek