【问题标题】:How to grep values after pattern in string如何在字符串中的模式后grep值
【发布时间】:2015-10-25 18:01:56
【问题描述】:

我需要通过 grep 从此字符串中获取值:

<person_data>
    <person record_id="9209310" actual_date="2015-03-27" type="1" hash_last_name="TEST" hash_first_name="NAME" hash_middle_name="OTHER" hash_birth_date="1989-01-12" sex="2" marital_status="4" is_dead="2">
      <person_docs>
        <doc record_id="9257172" actual_date="2015-03-27" doc_type="1" hash_doc_serial="1234" hash_doc_number="7584933" hash_doc_issue_date="OCT 29 2016 13:00AM" doc_issue_auth="" />

我想在模式后获取值 - hash_

例如,我想从这个字符串中获取值 - TEST、NAME、OTHER、1989-01-12:

<person record_id="9209310" actual_date="2015-03-27" type="1" hash_last_name="TEST" hash_first_name="NAME" hash_middle_name="OTHER" hash_birth_date="1989-01-12" sex="2" marital_status="4" is_dead="2">

怎么做?我可以使用 grep 或 sed 吗?

【问题讨论】:

    标签: regex sed grep pattern-matching


    【解决方案1】:

    使用后视:

    grep -Po 'hash_[^"]*"\K[^"]*' file
    

    这会查找像 hash_ 这样的字符串 + 直到 " 的字符串,并打印之后的内容,直到找到 "。

    测试

    $ grep -Po 'hash_[^"]*"\K[^"]*' <<< '<person record_id="9209310" actual_date="2015-03-27" type="1" hash_last_name="TEST" hash_first_name="NAME" hash_middle_name="OTHER" hash_birth_date="1989-01-12" sex="2" marital_status="4" is_dead="2">'
    TEST
    NAME
    OTHER
    1989-01-12
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      相关资源
      最近更新 更多