質問

このように見えるXMLファイルがあります。

<species compartment="compartment" id="alpha_dash_D_dash_glucose_dash_6P" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="six_dash_Phospho_dash_D_dash_gluconate" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="beta_dash_D_dash_Fructose_dash_6P2" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="beta_dash_D_dash_Glucose" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>

id 属性私は自分の属性に置き換えたいです。エンドファイルを次のようにしたい:

<species compartment="compartment" id="id1" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="id2" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="id3" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">
     </species>
     <species compartment="compartment" id="id4" initialAmount="0" hasOnlySubstanceUnits="true" constant="false" boundaryCondition="false">

しかし id 属性は、ファイル内の他の場所で参照されます。

 <speciesReference constant="true" stoichiometry="1" species="alpha_dash_D_dash_glucose_dash_6P">

この行は次のように更新する必要があります。

 <speciesReference constant="true" stoichiometry="1" species="id1">

使ってみました sed's/id="(*)"/id="$IdCOUNTER"/g' しかし、これはすべてを作ります id 属性も同じです。どうすればこれを解決できますか?どんな助けも感謝しています、ありがとう。

役に立ちましたか?

解決

sed -n 's/\s*<species [^>]* id="\([^"]*\).*/\1/p' species.xml |\
  cat -n |\
  sed 's/\s*\([0-9]\+\)\s*/id\1 /' > ids.txt

cp species.xml my_species.xml

while read a b
do
  sed -i 's/"'"$b"'"/"'$a'"/g' my_species.xml
done < ids.txt

XMLファイルがうまくフォーマットされていると仮定すると(つまり、各タグはすべて1行にあります)、SEDとBashで逃げることができます。それ以外の場合は、XMLパーサーを備えた言語が必要です。同じアプローチが機能しますが、詳細は異なります。

交換用のIDのマップを作成します。次に、以前に見たIDに遭遇するたびに、調べて交換します。

sed 上記の行は、aから各IDをマッピングします <species> 番号付きIDにタグを付けます(バックスラッシュにより、読みやすくするためにいくつかの行にラインを分割できます)。

ファイルは、オリジナルの変更を防ぐためにコピーされます。

各行がIDマップファイルから読み取られるため、元のIDのすべての発生は、新しい番号付きIDに置き換えられます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top