【问题标题】:shell script to extract multiline data out of a text file with regex使用正则表达式从文本文件中提取多行数据的 shell 脚本
【发布时间】:2012-06-26 10:25:20
【问题描述】:

我正在尝试使用带有 shell 脚本的正则表达式从文本文件中提取一些特定数据

这是使用多行 grep .. 而我使用的工具是 pcregrep,这样我就可以与 perl 的正则表达式兼容

 [58]Walid Chamoun Architects WLL
     * [59]Map
     * [60]Website
     * [61]Email
     * [62]Profile
     * [63]Display Ad

   Walid Chamoun Architects WLL

   PO Box:
          55803, Doha, Qatar

   Location:
          D-Ring Road, New Salata Shamail 40, Villa 340, Doha, Qatar

   Tel:
          (00974) 44568833

   Fax:
          (00974) 44568811

   Mob:
          (00974) 44568822

     * Accurate Budget Costing
     * Eco-Friendly Structural Design
     * Exclusive & Unique Design
     * Quality Architecture & Design

Company Profile

   Walid Chamoun Architects (WCA) was founded in Beirut, Lebanon, in 1992,
   committed to the concept of fully integrated design-build delivery of
   projects. In late '90s, company established in-house architectural and
   engineering services. As a full service provider, WCA expanded from
   multi-family projects to industrial and office construction, which
   added development services, including site acquisition and financing.
   In 2001, WCA had opportunity and facilities to experience European
   market and establish office in Puerto Banus, Marbella, Spain. By 2005,
   WCA refined its structure to focus on specific market segments and new
   office was opened in Doha, state of Qatar. From a solid foundation and
   reputation built over eighteen years, WCA continually to provide
   leadership in design-build through promotion of benefits and education
   to its practitioners.
   Project Planning: Project planning and investigation occurs before
   design begins has greatest impact on cost, schedule and ultimately the
   success of project. Creativity in Design: You can rely on our in-house
   designers for design excellence in all aspects of the project. Our
   designs have received recommendations and appreciations on national and
   international levels. Creativity in Execution: Experienced in close
   collaboration with the designers as part of the integrated team, our
   construction managers, superintendents and field staff create value
   throughout the project. Post Completion Services: Your needs can be
   served through our skills and experience long after the last
   construction crew has left the site. Performance: Corporate and
   institutional clients, developers and public agencies repeatedly select
   WCA on the basis of its consistent record of performance excellence.
   Serving clients throughout the Middle East and GCC, WCA provides
   complete planning for architectural, interior design and construction
   on a single-responsibility basis. Our expertise spans industrial,
   commercial, institutional, public and residential projects. Benefits of
   Design-Build: Design-build is a system of contracting under which one
   entity performs both design and construction. Benefits of design-build
   project delivery include: Single point responsibility Early knowledge
   of cost Time and Cost savings

   Classification:
          Architects - [64]Architects

   [65]Al Ali Consulting & Engineering
     * [66]Map
     * Website
     * Email
     * Profile
     * Display Ad

   Is this your company?
   [67]Upgrade this free listing here

   PO Box:
          467, Doha, Qatar

   Tel:
          (00974) 44360011

Company Profile

   Classification:
          Architects - [68]Architects

   [69]Al Gazeerah Consulting Engineering
     * [70]Map
     * Website
     * Email
     * Profile
     * Display Ad

   Is this your company?
   [71]Upgrade this free listing here

   PO Box:
          22414, Doha, Qatar

   Tel:
          (00974) 44352126

Company Profile

   Classification:
          Architects - [72]Architects

   [73]Al Murgab Consulting Engineering
     * [74]Map
     * Website
     * Email
     * Profile
     * Display Ad

   Is this your company?
   [75]Upgrade this free listing here

   PO Box:
          2856, Doha, Qatar

   Tel:
          (00974) 44448623

Company Profile

   Classification:
          Architects - [76]Architects
References

   Visible links
   1. http://www.qatcom.com/useraccounts/login
   2. http://www.qatcom.com/useraccounts/register
   3. http://www.qatcom.com/
   4. http://www.qatcom.com/
   5. http://www.qatcom.com/qataryellowpages/map-of-doha
   6. http://www.qatcom.com/qataryellowpages/about-qatcom
   7. http://www.qatcom.com/qataryellowpages/advertise-with-qatcom
   8. http://www.qatcom.com/qataryellowpages/advertiser_testimonials
   9. http://www.qatcom.com/useraccounts/login
  10. http://www.qatcom.com/useraccounts/register
  11. http://www.qatcom.com/contact-qatcom
  12. http://www.qatcom.com/qataryellowpages/companies
  13. http://www.qatcom.com/classifications/index/A
  14. http://www.qatcom.com/classifications/index/B
  15. http://www.qatcom.com/classifications/index/C
  16. http://www.qatcom.com/classifications/index/D
  17. http://www.qatcom.com/classifications/index/E
  18. http://www.qatcom.com/classifications/index/F
  19. http://www.qatcom.com/classifications/index/G
  20. http://www.qatcom.com/classifications/index/H
  21. http://www.qatcom.com/classifications/index/I
  22. http://www.qatcom.com/classifications/index/J
  23. http://www.qatcom.com/classifications/index/K
  24. http://www.qatcom.com/classifications/index/L
  25. http://www.qatcom.com/classifications/index/M
  26. http://www.qatcom.com/classifications/index/N
  27. http://www.qatcom.com/classifications/index/O
  28. http://www.qatcom.com/classifications/index/P

对于这样的样本数据,我正在尝试获取公司的详细信息

company name
po box
Tel
fax
mobile
company profile 

到 .csv 文件中 我也是正则表达式和linux的新手.. 我能设法得到的就是这样的东西

\[\d*\][^\.]*[\(\d*\)\s\d*)]

谁能帮我解决这个问题..

改进:

我想出了类似的东西

$ awk '/^\[/ && ! /Upgrade this free listing/ {print $0} /:$/ && ! /Classification/ {printf $0 ;  getline x ; print x}' file

但这仍然不是我想要的......

【问题讨论】:

  • 能否请求不同格式的数据文件?这种格式易于阅读,但不易解析……
  • 我使用“lynx -dump”命令下载了数据..所以这是我可以获取数据的唯一格式! @hovanessyan
  • 你能检查一下“wget -O somefile www.exmple.com”的输出,看看是否更方便解析?
  • 当然.. 给我一秒钟@hovanessyan
  • 是的,我知道输出的外观,但我的意思是,现在您可以使用脚本语言和库以编程方式解析它。例如 ruby​​+nokogiri 或 python+html.parser 模块等,如果您不熟悉这些语言中的任何一种,这是一个很好的入门机会。您甚至可以使用您喜欢的任何其他语言开发解析器(基于标签的名称)...

标签: linux shell awk grep


【解决方案1】:

您可以在 awk 中执行此操作,但最好改为解析 HTML。使用Beautiful Soup 模块的Python 是一个很好的工具。但这不是很令人兴奋,所以这里是如何用尴尬(哈哈!)的方式来做:

#!/usr/bin/awk -f

function trim(s) {
    gsub(/(^ +)|( +$)/, "", s)
    return s
}

BEGIN {
    count = 0
    fields[0] = "company"
    fields[1] = "pobox"
    fields[2] = "tel"
    fields[3] = "fax"
    fields[4] = "mob"
    fields[5] = "profile"
}

# company name
/^ +\[[0-9]+\].*$/ {
    sub(/^ +\[[0-9]+\]/, "") # get rid of the Lynx reference
    # this is a bit naughty: our regex also matches this other link, but there's only one of them, so we just filter it
    if ($0 != "Upgrade this free listing here") data[count,"company"]=$0
}

# two line fields, easy!
/ +PO Box:$/ { getline; data[count,"pobox"]=$0 }
/ +Tel:$/ { getline; data[count,"tel"]=$0 }
/ +Fax:$/ { getline; data[count,"fax"]=$0 }
/ +Mob:$/ { getline; data[count,"mob"]=$0 }

# multi-line field, tricky because it can be empty
/^Company Profile$/ {
    getline # skip empty line

    # process lines until encountering Classification field
    s = ""
    do {
        s = s $0
        getline
    } while ($0 !~ / +Classification:$/)
    data[count,"profile"]=s
    count++ # the Classification field denotes the end of the company record
}

END {
    OFS=","

    # output CSV header row
    for ( key in fields ) {
        printf "\"" fields[key] "\","
    }
    printf "\n"

    # output data
    for ( i=0; i<count; i++ ) {
        for ( key in fields ) {
            printf "\"" trim(data[i,fields[key]]) "\","
        }
        printf "\n"
    }
}

另存为parse.awk,然后使用./parse.awk &lt; sample.txt 调用。输出一个 CSV,如下所示:

"tel","fax","mob","profile","company","pobox",
"(00974) 44568833","(00974) 44568811","(00974) 44568822","Walid Chamoun Architects (WCA) was founded in Beirut, Lebanon, in 1992,   committed to the blablabla","Walid Chamoun Architects WLL","55803, Doha, Qatar",
"(00974) 44360011","","","","Al Ali Consulting & Engineering","467, Doha, Qatar",
"(00974) 44352126","","","","Al Gazeerah Consulting Engineering","22414, Doha, Qatar",
"(00974) 44448623","","","","Al Murgab Consulting Engineering","2856, Doha, Qatar",

有希望解释发生了什么的 cmets。这将在普通的旧 awk 中运行,不需要花哨的 gawk 功能。请记住,awk 数组是任意排序的。这很容易用不同的输入数据破坏一大堆,这只是您真正应该解析 HTML 而不是 lynx -dump 恶作剧的众多原因之一。

【讨论】:

  • 谢谢哥们!这救了我的命 :) 现在我必须努力学习 python 和珍珠... :)
猜你喜欢
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2018-01-18
  • 2020-01-29
  • 1970-01-01
相关资源
最近更新 更多