一个正则表达式来grep文件的特定段落 [英] a regular expression to grep specific paragraphs of a file

查看:97
本文介绍了一个正则表达式来grep文件的特定段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  
假设这是我的shell脚本运行的数据

所有权
o澳大利亚所有

采矿工程师的广告
232美元的结果
所有州的采矿工程师
由采矿工程师[x]类别过滤
* []
[34]获取路线
类别:
[35]采矿工程师
[36] Arrow电气服务公司在新南威尔士州卧龙岗市根据矿业
工程师标志
[37]电子邮件
[38]发送给手机
[39]信息
比较(0)
* []
。 [40] Firefly国际
设计师&制造商。服务,修理&聘请。
我们是新南威尔士州Mt Thorley的采矿工程师的供应商。
25 Thorft Cl,Mt Thorley NSW 2330
ph:(02)6574 6660
[41] http://www.fireflyint.com.au
[42]获取路线
类别:
[43]采矿工程师
[44]采矿工程师在NSW Thorley的Firefly International
logo
[45]电子邮件
[ 46]发送到手机
[47]信息
比较(0)
* []
[48]材料解决方案
Materials Research&开发,泥浆流变学和管道设计。
我们是一家成熟的公司,为采矿业和矿业提供服务。
所有地区的相关制造业。
Thornlie WA 6108
电话:(08)6468 4118
[49] www.materialssolutions.com.au
类别:
[50]采矿工程师
[51]采矿工程师在华盛顿Thornlie的材料解决方案logo
[52]电子邮件
[53]发送到手机
[54]信息
比较(0)
* []
。 [55] ATC Williams Pty Ltd
我们的服务可以从概念到完成作品。
今天,作为重新命名的ATC Williams,我们继续扩大澳大利亚和全球各地的
业务。
Unit 1,21 Teddington Rd,Burswood WA 6100
电话:(08)9355 1383
[56] www.atcwilliams.com.au
[57]获取路线
类别:
[58]采矿工程师
[59] ATC Williams Pty Ltd在矿业工程师处的华盛顿Burswood工厂
logo
[60] email
[61 ]发送到手机
[62]信息
比较(0)

和我需要抓住看起来像这样的地址

  * [] 
。 [55] ATC Williams Pty Ltd
我们的服务可以从概念到完成作品。
今天,作为重新命名的ATC Williams,我们继续扩大澳大利亚和全球各地的
业务。
单元1 21 Teddington Rd,Burswood WA 6100
ph:(08)9355 1383
[56] www.atcwilliams.com.au

那我该怎么做..
我一直在处理像

的正则表达式


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


但那不是帮助..它匹配的地址,当我给与我想要的地址匹配的输入文件..但是当批量给出时,它没有帮助。
,所以有人可以帮我解决问题。

解决方案

试试这个

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

解释

  ^#在行首(位于字符串的开头或换行符后)的断言位置
\s#匹配单个字符是一个空白字符(空格,制表符和换行符)
*#在零和无限次之间,尽可能多地根据需要返回(贪婪)
\ *#字面上匹配字符*
\s#匹配一个空白字符(空格,制表符和换行符)的单个字符
*#在零和无限次之间,多次(贪婪)
\ [[#字符串匹配
\#匹配字符字面意思
\]#匹配字符]字面意思
[^ \ *]#匹配任何不是字符
+的字符? #在一次和无限次之间,尽可能少的次数,根据需要扩展(懒惰)
[。]#匹配字符。
com#字符串匹配字符com
[。]#匹配字符。
au#将字符au逐字匹配
$#在行末尾(位于字符串末尾或换行符前) )


hi i am working on a shellscript.. suppose this is the data my shell script runs on

      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)

and i need to grab addresses that look like this

 * [ ]
       . [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

so what do i do.. i've been working on regular expressions like

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

but thats not helping.. it matches the address when i give the input file with the address match i want.. but when given in bulk, it doesnt help. so can somebody help me out..

解决方案

Try this

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

explanation

^        # 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)

这篇关于一个正则表达式来grep文件的特定段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆