문제

다음과 같은 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 파일이 멋지게 형식화되어 있다고 가정하면 (즉, 각 태그가 모두 한 줄에 있음) SED와 BASH로 도망 갈 수 있습니다. 그렇지 않으면 XML 파서가있는 언어가 필요합니다. 동일한 접근 방식이 작동하지만 세부 사항은 다릅니다.

교체 할 ID지도를 만듭니다. 그런 다음 이전에 본 ID를 만날 때마다 찾아서 교체합니다.

그만큼 sed 위의 줄은 각 ID를 a에서 맵핑합니다 <species> 번호가 매겨진 ID로 태그를 지정합니다 (백 슬래시를 사용하면 가독성을 위해 줄을 여러 줄에 분할 할 수 있습니다).

원본 수정을 방지하기 위해 파일이 복사됩니다.

각 줄이 ID 맵 파일에서 읽히면 원래 ID의 모든 발생이 새 번호가 매겨진 ID로 대체됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top