2010-07-17 107 views
1

我有一個奇怪的問題,使用awk正則表達式匹配替換XML文件中的一些文本。這個awk正則表達式替換有什麼問題?

xml文件很簡單。每個xml的節點中都有一段文本,awk程序會將該文本替換爲從文本文件rtxt中選取的另一段文本。但由於某些原因,替換42.xml中文本的rtxt中的文本(標記爲'42')不會產生適當的替換。

toxml.awk寫入標準輸出。它首先打印xml,因爲它讀取了它,然後是最終的替換結果。

我實際上有一個這些XML文件的集合,我用一個更長的rtxt文本替換文本。恰巧這個特定的替換(對於42.xml)不起作用。替代元素中的文本被替換,另一個標籤嵌套在現有的標籤中。


toxml.awk

BEGIN{ 
    srcfile = "rtxt" 
    FS = "|" 

    while (getline <srcfile) { 
    xmlfile = $1 ".xml" 
    rep = "<narrative>" $2 "</narrative>" 

    ## read in the xml file in one go. 
    ## (the last tag will be missing.) 
    RS = "</topic>" 
    FS = "</topic>" 

    getline <xmlfile 
    #print $0 
    close(xmlfile) 

    ## replace 
    subs = gsub(/<narrative>.*<\/narrative>/, rep, $0) 

    ## append the closing tag 
    subs = gsub(/[ \n\r\t]+$/, "\n</topic>", $0) 
    print $0 

    ## restore them before reading rtxt. 
    RS = "\n" 
    FS = "|" 
    } 

    close(srcfile) 
} 

rtxt

42 |信息顯示爲Java培訓機構的詳細信息,以及IT公司,提供Java解決方案也被認爲是不相關的。 Java是Sun Microsystems開發的流行編程語言。我有興趣瞭解這種編程語言,並學習編程。爲了保持相關性,結果應提供有關不同版本的Java和Java中不同概念的Java &的歷史信息。它很好,如果我找到學習Java的教程。僅與Sun Microsystems相關但不與Java相關的結果被認爲是不相關的。我喜歡找到討論這種編程語言的文章以及各種版本的概念。


42.xml

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE topic SYSTEM "topic.dtd"> 
<topic id="2009042" ct_no="227"> 

    <title>sun java</title> 

    <castitle>//article[about(.//language, java) or about(.,sun)]//sec[about(.//language, java)]</castitle> 

    <phrasetitle>"sun java"</phrasetitle> 

    <description>Find information about Sun Microsystem's Java language</description> 

    <narrative>Java is a popular programming language developed at Sun Microsystems. I am interested to know about this programming language, and also to learn programming with it. To be relevant, a result should give information on history of Java &amp; on different versions of Java, and on different concepts in Java. Its good if I find tutorials for learning Java. Results related only to Sun Microsystems but not Java are considered non-relevant. Results showing details of training institutes for Java, and IT companies which provide Java solutions are also considered non-relevant. I like to find articles that discuss this programming language and various concepts &amp; versions of it. </narrative> 

</topic> 

+4

我不知道別人,但我不起來的只是下載和解壓文件回答一個問題。改爲使用pastebin服務。 – 2010-07-17 06:11:42

+0

即使我認爲這不會是正確的方式問在Stackoverflow,但我沒有其他方式來演示問題。提供4個文件的鏈接將是另一個混​​亂。我需要找人查看文本文件並運行程序。我必須找到其他方法。 – rup 2010-07-17 06:27:10

+0

有一點似乎很明顯,就是你的getline需要循環。 – 2010-07-17 09:50:20

回答

0

只是一個開始

#!/bin/bash 

awk 'BEGIN{FS="|"} 
FNR==NR{ nar[$1]=$2; next } 
END{ 
    for(i=2;i<ARGC;i++){ 
    xmlfile=ARGV[i] 
    split(xmlfile,fname,".") 
    print "Doing file: "xmlfile 
    print "---------------------------------" 
    while((getline line < xmlfile) > 0) { 
     if (line ~ /<narrative>/){ 
      line="<narrative>"nar[fname[1]]"</narrative>" 
     } 
     print line 
    } 
    } 
}' rtxt 42.xml 71.xml 
+0

修改它。看一看。 – rup 2010-07-17 08:31:47

相關問題