2009-04-25 5 views
1

Hallo Leute fragen, wenn Sie mir helfen könnten Ich versuche, ein Bash-Skript zu kompilieren, die einige Werte aus einem Abschnitt von HTML-Code anzeigen wird und ich bin auf der regulären fest Teil AusdruckRegulärer Ausdruck Grabbing X Menge an Werten in Linux Bash

ich habe folgende Stück Code

<li><div friendid="107647498" class="friendHelperBox"><div><a href="http://www.myspace.com/rockyrobsyn" class="msProfileTextLink" title="rØbylin">rØbylin</a></div><span class="msProfileLink friendToolTipBox" friendid="107647498" style="width:90px;"><a href="http://www.myspace.com/rockyrobsyn"><img src="http://x.myspacecdn.com/modules/common/static/img/spacer.gif" source="http://c2.ac-images.myspacecdn.com/images01/59/s_8b94c89a98de643e59ab9a1cf03885c1.jpg" alt="rØbylin" class="profileimagelink" onerror="UseNoPicImage(event.target||event.srcElement)" /><span class="pilRealName">Robyn</span></a></span></div><br /><img src="http://x.myspacecdn.com/images/onlinenow.gif" /></li><li><div friendid="59261168" class="friendHelperBox"><div><a href="http://www.myspace.com/christownsendmusic" class="msProfileTextLink" title="Chris Townsend">Chris Townsend</a></div><span class="msProfileLink friendToolTipBox" friendid="59261168" style="width:90px;"><a href="http://www.myspace.com/christownsendmusic"><img src="http://x.myspacecdn.com/modules/common/static/img/spacer.gif" source="http://c4.ac-images.myspacecdn.com/images02/83/s_029c098cc40c40ff8f88fe54d53a1277.jpg" alt="Chris Townsend" class="profileimagelink" onerror="UseNoPicImage(event.target||event.srcElement)" /></a></span></div><br /><img src="http://x.myspacecdn.com/images/onlinenow.gif" /></li></ul> 

alle auf einer Linie, und ich möchte den Text alle ziehen, die im Inneren ist

..class="msProfileTextLink" title="<GRAB THIS TEXT>">.... 

würde ich Ich mag es, alle Ereignisse zu erfassen, wie kann ich das tun?

Antwort

1

ich gehe davon aus, dass es okay ist, Standard-Unix-Tools aufrufen, nicht nur bash Einbauten

Nun,

grep -o 'class="msProfileTextLink" title="([^"])*"' file.html

bekommt man so weit:

class="msProfileTextLink" title="rØbylin"

class="msProfileTextLink" title="Chris Townsend"

Das es wird davon ausgegangen ist nie Leerzeichen Variation in der html - sonst müssen Sie

tun

egrep -o 'class="msProfileTextLink"[[:space:]]*title="([^"])*"' Einfügen der überall wo es einige Leerzeichen geben könnte.

Dann grep -o '"[^"]*"$' es wird nach unten zu:

"rØbylin"

"Chris Townsend"

1

Was ist mit Perl? ;)

#!/usr/bin/perl 

$string = 'Your string'; 

$string =~ m/class=\"msProfileTextLink\" title=\"([^\"]*)\"/; 

print $1; print "\n"; 
1

Der folgende Perl-Stil regex für Sie funktionieren soll:

m/class="msProfileTextLink"\s*title="([^"]+)"/g 

Soweit es aus einem Bash-Skript verwenden, sollten Sie in der Lage sein, es in einem Perl-Einzeiler zu verwenden (siehe -p und -ePerl command-line options) oder in einer anderen Sprache, die perl-Stil reguläre Ausdrücke wie Python unterstützt, PHP usw.

1

Versuchen Sie, diese

awk '/title="([^"]*)"/ {print substr($2,8,length($2)-8)}'