2

私はこのファイル(dev1.temp)を持っています:

 <?xml version="1.0" encoding="UTF-8"?>
<krpano version="1.0.8.15" showerrors="false">

          <include url="include/sa/index.xml" /> <include url="content/sa.xml" />
          <include url="include/global/index.xml" />
          <include url="include/orientation/index.xml" />
          <include url="include/movecamera/index.xml" /> <include url="content/movecamera.xml" />
          <include url="include/fullscreen/index.xml" />
          <include url="include/instructions/index.xml" />
          <include url="include/coordfinder/index.xml" />
          <include url="include/editor_and_options/index.xml" />
</krpano>

目標は、すべてのURLのコンテンツを取得し、それらを一時ファイル(devel.temp)に配置することです。出力は次のようになります。

include/sa/index.xml
content/sa.xml
include/global/index.xml
include/orientation/index.xml
include/movecamera/index.xml
content/movecamera.xml
include/fullscreen/index.xml
include/instructions/index.xml
include/coordfinder/index.xml
include/editor_and_options/index.xml

トリックを行うために、私は次のスクリプトを持っています:

# Make a temp file with all the files url's    
grep -o 'url=['"'"'"][^"'"'"']*['"'"'"]' $temp_folder"/devel1.temp" > $temp_folder"/devel2.temp"
# Strip off everything to leave just the url's'    
sed -e 's/^url=["'"'"']//' -e 's/["'"'"']$//' $temp_folder"/devel2.temp" > $temp_folder"/devel.temp"

昨日は完璧に機能しました。今日、devel2.tempとdevel.tempの出力は次のとおりです。

[01;31m[Kurl="include/sa/index.xml"[m[K
[01;31m[Kurl="content/sa.xml"[m[K
[01;31m[Kurl="include/global/index.xml"[m[K
[01;31m[Kurl="include/orientation/index.xml"[m[K
[01;31m[Kurl="include/movecamera/index.xml"[m[K
[01;31m[Kurl="content/movecamera.xml"[m[K
[01;31m[Kurl="include/fullscreen/index.xml"[m[K
[01;31m[Kurl="include/instructions/index.xml"[m[K
[01;31m[Kurl="include/coordfinder/index.xml"[m[K
[01;31m[Kurl="include/editor_and_options/index.xml"[m[K

何が起こっているのかについて何かアイデアはありますか?

4

4 に答える 4

3

xpathなどのxmlターゲットツールの使用を検討してください。私はこれを提案します:

xpath -e "/krpano/include/@url" -q yourFile.xml | cut -f 2 -d "=" | sed 's/"//

krpanoxmlにルートがあり、include'sのみがurl属性を持っていることが確実な場合。以下を省略形として使用することもできますが、上記の方が高速に実行されます。

xpath -e "//@url" -q yourFile.xml | cut -f 2 -d "=" | sed 's/"//
于 2012-10-02T10:33:50.420 に答える
2

grep出力が端末でない場合でも、ANSIシーケンスを使用して出力に色を付けるようです。--colorからalwaysに変更しautoます。

XMLの処理に使用するのではなくgrep、XML対応ツールを使用する必要があります。たとえば、xshでは、次のように書くことができます。

open file.xml ;
perl { use Term::ANSIColor } ;
for /krpano/include
    echo :s { color('bright_yellow') }
            @url
            { color('reset') } ;
于 2012-10-02T10:17:22.810 に答える
2

ちょろばさんのコメントに加えて再。ANSIシーケンスの場合、可能な限りsedなどを介してXMLを解析することは避け、XML対応のスクリプトツールを使用することを検討します。XMLStarletツールキットを使用しています。これは、スクリプトが文字エンコード/エンティティを認識し、XMLの変更に直面してもより堅牢であることを意味します。

于 2012-10-02T10:19:59.567 に答える
1

3番目のxml対応スクリプトツールは私のXidelです:

xidel /tmp/your.xml -e //@url

(ほとんどの場合とは異なり、XPath 2.0をサポートしていますが、この問題ではやり過ぎです)

于 2012-10-02T18:56:52.107 に答える