2009-04-25 6 views
1

皆さんが私を助けてくれるのではないかと思っています。htmlコードの一部の値を表示するbashスクリプトをコンパイルしようとしています。表現の一部、正規表現grabbing X値がlinuxのbashで返ってくる

は、私は1つの行にすべてのコード

<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> 

の以下の部分を持っていると私は

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

内にあるすべてのテキストを引き出したいIだろうどのように私はこれを行うことができますすべての出現をつかむように?

答えて

1

で、想定してい

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

class="msProfileTextLink" title="Chris Townsend"

がで空白ばらつきがあることはない:だけでなく、BASH組み込みコマンド

まあ、

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

は限りあなたを取得しますhtml - そうでなければあなたがする必要があります

egrep -o 'class="msProfileTextLink"[[:space:]]*title="([^"])*"' [[space]]*を挿入すると、空白があることがあります。

その後grep -o '"[^"]*"$' はそれを取得する:

"rØbylin"

"クリス・タウンゼント"

1

Perlについてはどうですか? ;)

#!/usr/bin/perl 

$string = 'Your string'; 

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

print $1; print "\n"; 
1

次のPerlスタイルの正規表現は、あなたのために働く必要があります。

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

限りbashスクリプトから、それを使用して、あなたはPerlのワンライナーでそれを使用することができるはずです(-p-ePerl command-line optionsを参照)、または私はそれが標準的なUnixツールを起動しても大丈夫だと仮定しているなど、そのようなPythonやPHPなどのPerlスタイルの正規表現をサポートしている別の言語、

1

この

awk '/title="([^"]*)"/ {print substr($2,8,length($2)-8)}' 
をお試しください