簡體   English   中英

使用awk從xml元素中提取文本

[英]extract text from xml elements using awk

我有一個大約 10k 這種類型的 xml 標簽的文件:

<!-- http://purl.obolibrary.org/obo/HP_0100516 -->

<owl:Class rdf:about="http://purl.obolibrary.org/obo/HP_0100516">
    <obo:IAO_0000115 rdf:datatype="http://www.w3.org/2001/XMLSchema#string">The presence of a neoplasm of the ureter.</obo:IAO_0000115>
    <oboInOwl:created_by rdf:datatype="http://www.w3.org/2001/XMLSchema#string">doelkens</oboInOwl:created_by>
    <oboInOwl:creation_date rdf:datatype="http://www.w3.org/2001/XMLSchema#string">2010-12-20T10:35:11Z</oboInOwl:creation_date>
    <oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">UMLS:C0041955</oboInOwl:hasDbXref>
    <oboInOwl:hasRelatedSynonym rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Neoplasia of the ureters</oboInOwl:hasRelatedSynonym>
    <oboInOwl:hasRelatedSynonym>ureter, cancer of</oboInOwl:hasRelatedSynonym>
    <oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">HP:0100516</oboInOwl:id>
    <rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Neoplasm of the ureter</rdfs:label>
</owl:Class>
<owl:Axiom>
    <owl:annotatedSource rdf:resource="http://purl.obolibrary.org/obo/HP_0100516"/>
    <owl:annotatedProperty rdf:resource="http://purl.obolibrary.org/obo/IAO_0000115"/>
    <owl:annotatedTarget rdf:datatype="http://www.w3.org/2001/XMLSchema#string">The presence of a neoplasm of the ureter.</owl:annotatedTarget>
    <oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">HPO:probinson</oboInOwl:hasDbXref>
</owl:Axiom>

我想轉換為只有 2 個 xml 元素的制表符分隔文本文件:

Neoplasm of the ureter  The presence of a neoplasm of the ureter

通過使用awk

我需要提取的文本在這些標簽內:

<obo:IAO_0000115 rdf:datatype="http://www.w3.org/2001/XMLSchema#string">The presence of a neoplasm of the ureter.</obo:IAO_0000115>

<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Neoplasm of the ureter</rdfs:label>

以及我打算使用的 awk 腳本:

BEGIN{RS="//"}
{
  match($0, regex1 , a)
  match($0, regex2, b)
  print a[1], "\t", b[1]
}

使用正則表達式獲取 xml 元素內的文本的最佳方法是什么?

注意:這種方法非常有用,並證明了 awk 可用於從復雜的 xml/rdf 結構中提取 xml 文本

感謝@RavinderSingh13 使用的最終 awk 腳本:

awk '
/obo:IAO_0000115 rdf:datatype/ && match($0,/>.*</,a){
  gsub(/^>|<$/,"",a[0])
  
}
/rdfs:label rdf:datatype/ && match($0,/>.*</,b){
  gsub(/^>|<$/,"",b[0])
  print b[0]"\t"a[0]
}
'  file.xml > output.txt

您能否僅根據您顯示的樣本嘗試以下操作。 awk也不是 xml 解析的理想工具,因為 OP 特別提到 OP 不能使用任何其他工具,所以在這里使用這種方法。

awk '
(/obo:IAO_0000115 rdf:datatype/ || /rdfs:label rdf:datatype/) && match($0,/>.*</){
  print substr($0,RSTART+1,RLENGTH-2)
}
'  Input_file

說明:為以上添加詳細說明。

awk '                                         ####Starting awk program from here.
(/obo:IAO_0000115 rdf:datatype/ || /rdfs:label rdf:datatype/) && match($0,/>.*</){    ####Chcecking condition if line contains obo:IAO_0000115 rdf:datatype OR rdfs:label rdf:datatype AND matches everythig from > to till < in current line.
  print substr($0,RSTART+1,RLENGTH-2)         ####Printing sub-string from RSTART to till RLENGTH here, where RSTART and RLENGTH variables are set whenever a match function has TRUE/matched regex in it.
}
'  Input_file                                 ####Mentioning Input_file here.

man awk

RSTART match() 匹配的第一個字符的索引; 如果不匹配,則為 0。 (這意味着字符索引從 1 開始。) RLENGTH match() 匹配的字符串的長度; -1 如果沒有匹配。



編輯:根據 OP 的評論再添加 1 個解決方案,以防有人想從 2 個不同的字符串搜索中創建 2 個不同的數組,然后嘗試以下操作。 在 GNU awk編寫和測試。

awk '
/obo:IAO_0000115 rdf:datatype/ && match($0,/>.*</,a){
  gsub(/^>|<$/,"",a[0])
  print a[0]
}
/rdfs:label rdf:datatype/ && match($0,/>.*</,b){
  gsub(/^>|<$/,"",b[0])
  print b[0]
}
'  Input_file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM