2012-06-13 41 views
0

你好我在shell腳本的工作.. 想這是我的shell腳本上正則表達式到grep一個文件的具體段落

 Ownership 
       o Australian Owned 
    ? 
    Ads for Mining Engineers 
    232 results for 
mining engineers in All States 
    filtered by Mining Engineers [x] category 
    * [ ] 
        [34]get directions 
     Category: 
     [35]Mining Engineers 
     [36]Arrow Electrical Services in Wollongong, NSW under Mining 
     Engineers logo 
      [37]email 
      [38]send to mobile 
      [39]info 
      Compare (0) 
    * [ ] 
     . [40]Firefly International 
     Designers & Manufacturers. Service, Repair & Hire. 
     We are the provider of mining engineers in Mt Thorley, NSW. 
     25 Thrift Cl, Mt Thorley NSW 2330 
     ph: (02) 6574 6660 
      [41]http://www.fireflyint.com.au 
      [42]get directions 
     Category: 
     [43]Mining Engineers 
     [44]Firefly International in Mt Thorley, NSW under Mining Engineers 
     logo 
      [45]email 
      [46]send to mobile 
      [47]info 
      Compare (0) 
    * [ ] 
     [48]Materials Solutions 
     Materials Research & Development, Slurry Rheology & Piping Design. 
     We are a well established company servicing the mining industry & 
     associated manufacturing industries in all areas. 
     Thornlie WA 6108 
     ph: (08) 6468 4118 
      [49]www.materialssolutions.com.au 
     Category: 
     [50]Mining Engineers 
     [51]Materials Solutions in Thornlie, WA under Mining Engineers logo 
      [52]email 
      [53]send to mobile 
      [54]info 
      Compare (0) 
    * [ ] 
     . [55]ATC Williams Pty Ltd 
     Our services are available from concept to completion of the works. 
     Today, as the rebranded ATC Williams, we continue to expand our 
     operations across Australia and in locations around the world. 
     Unit 1, 21 Teddington Rd, Burswood WA 6100 
     ph: (08) 9355 1383 
      [56]www.atcwilliams.com.au 
      [57]get directions 
     Category: 
     [58]Mining Engineers 
     [59]ATC Williams Pty Ltd in Burswood, WA under Mining Engineers 
     logo 
      [60]email 
      [61]send to mobile 
      [62]info 
      Compare (0) 

運行數據,我需要抓住看起來像這樣的地址

* [ ] 
     . [55]ATC Williams Pty Ltd 
     Our services are available from concept to completion of the works. 
     Today, as the rebranded ATC Williams, we continue to expand our 
     operations across Australia and in locations around the world. 
     Unit 1, 21 Teddington Rd, Burswood WA 6100 
     ph: (08) 9355 1383 
      [56]www.atcwilliams.com.au 

所以我做了什麼.. 我一直工作在正則表達式像

^*(。?[\ W \ W?\ S?] *)+(。com.au)$

但那不是幫助..這時候我給輸入文件用的地址一致我想要的地址匹配..但是當批量給出時,它不起作用。 所以有人可以幫助我..

+0

,如果我用這麼長的表情,我的grep正在搜索元字符的特定字符..一樣,如果我的意思是\ S作爲任何空間,它搜索的字母「S」: ( –

回答

0

試試這個

^\s*\*\s*\[ \][^\*]+?[.]com[.]au$ 

解釋

^  # Assert position at the beginning of a line (at beginning of the string or after a line break character) 
\s  # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
    *  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
\*  # Match the character 「*」 literally 
\s  # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
    *  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
\[  # Match the character 「[」 literally 
\  # Match the character 「 」 literally 
\]  # Match the character 「]」 literally 
[^\*] # Match any character that is NOT a * character 
    +?  # Between one and unlimited times, as few times as possible, expanding as needed (lazy) 
[.]  # Match the character 「.」 
com  # Match the characters 「com」 literally 
[.]  # Match the character 「.」 
au  # Match the characters 「au」 literally 
$  # Assert position at the end of a line (at the end of the string or before a line break character) 
+0

實際看起來不錯! 但是當我嘗試使用該表達式的grep $ grep'^ \ s * \ * \ s * \ [\] [^ \ *] +?[。] com [。] au $'file 它給我沒有結果 但是當我在正則表達式測試儀中使用它..它肯定會工作.. 所以你能告訴我最新的問題嗎? @Cylian –

+0

@KiranVemuri:你忘了逃脫一個' *'剛剛在'^ \ s *'之後命令''$ grep'^ \ s ** \ s * [] [^ *] +?[。] com [。] au $''。將它改爲''grep'^ \ s * \ * \ s * [] [^ *] +?[。] com [。] au $''或'''$ grep'^ \ s * \ * \ s * [] [^ *] +?\。com \ .au $''。希望這可以工作。 – Cylian

+0

它就像我說的一樣.. 這個作品表達超細當我在一個正則表達式測試嘗試,但是當我在我的Linux終端用grep顯示它..它沒有給我結果:( –

1

我看到一些問題,你的正則表達式

^*(.?[\w\W?\s?]*)+(.com.au)$ 
^^  ^^^^
1 1   2 2 1 1 
  1. 特殊字符的是需要轉義

  2. 貪婪量詞,直到最後一場比賽的一切,「 .com.au「,在量詞之後添加一個?以使其不太可能匹配(意味着直到在行結尾找到的第一個」.com.au「)。

    ==>這是你的主要問題

  3. 你窩量詞*)+,你不需要那麼

  4. 在您的例子有「*」和「」之間的空白,所以無論是匹配空白還是刪除點,它都會與您的角色類相匹配。

  5. 也有該行的開始和之間的空白的 「*」

所以,試試這個

^\s*\*([\w\W?\s?]*?)(\.com\.au)$ 

看到它here on Regexr

+0

沒了..不工作.. 我最新的嘗試是

\*(\.?[\w\W?\s?]*)+([\w\W\s\d]*)?([\W\w]*\.\a\u)*$
這實際上是匹配所有的文字,而我只希望它的addrersses部分匹配! –

+0

如果我用這麼長的表情,我的grep正在搜索元字符的特定字符.. 像 如果我的意思是\ S作爲任何空間, 它搜索字母「S」 :( @stema –