2010-01-20 11 views
1

Referencer에 많은 참조가 있습니다. Referencer에서 내보낼 때 bibtex 파일에 파일 이름을 포함하려고합니다. 소프트웨어는 기본적으로이 작업을 수행하지 않기 때문에 XML 파일에 bibtex 정보로 파일 이름을 포함시키기 위해 sed 명령을 사용하여 내보내고 파일 이름을 포함하려고합니다.XML 태그 내에서만 바꾸기. Referencer .reflib에서 bib 명령을 사용하여 파일 이름이 그대로 유지되고 URL 인코딩이 제거 된 bibtex 형식으로 내보내기

입력

<doc> 
<filename>file:///home/dwickrama/Desktop/stevenJonesLab/papers/Transcription%20Factor%20Binding/A%20Common%20Nuclear%20Signal%20Transduction%20Pathway%20Activated%20by%20Growth%20Factor%20and%20Cytokine.pdf</filename> 
<relative_filename>A%20Common%20Nuclear%20Signal%20Transduction%20Pathway%20Activated%20by%20Growth%20Factor%20and%20Cytokine.pdf</relative_filename> 
<key>Sadowski93</key> 
<notes></notes> 
<bib_type>article</bib_type> 
<bib_doi></bib_doi> 
<bib_title>A common nuclear signal transduction pathway activated by growth factor and cytokine receptors.</bib_title> 
<bib_authors>Sadowski, H B and Shuai, K and Darnell, J E and Gilman, M Z</bib_authors> 
<bib_journal>Science</bib_journal> 
<bib_volume>261</bib_volume> 
<bib_number>5129</bib_number> 
<bib_pages>1739-44</bib_pages> 
<bib_year>1993</bib_year> 
<bib_extra key="pmid">8397445</bib_extra> 

OUPUT

<doc> 
<filename>file:///home/dwickrama/Desktop/stevenJonesLab/papers/Transcription%20Factor%20Binding/A%20Common%20Nuclear%20Signal%20Transduction%20Pathway%20Activated%20by%20Growth%20Factor%20and%20Cytokine.pdf</filename> 
<bib_extra key="File">article:../Transcription\ Factor\ Binding/A\ Common\ Nuclear\ Signal\ Transduction\ Pathway\ Activated\ by\ Growth\ Factor\ and\ Cytokine.pdf:pdf</bib_extra> 
<relative_filename>A%20Common%20Nuclear%20Signal%20Transduction%20Pathway%20Activated%20by%20Growth%20Factor%20and%20Cytokine.pdf</relative_filename> 
<key>Sadowski93</key> 
<notes></notes> 
<bib_type>article</bib_type> 
<bib_doi></bib_doi> 
<bib_title>A common nuclear signal transduction pathway activated by growth factor and cytokine receptors.</bib_title> 
<bib_authors>Sadowski, H B and Shuai, K and Darnell, J E and Gilman, M Z</bib_authors> 
<bib_journal>Science</bib_journal> 
<bib_volume>261</bib_volume> 
<bib_number>5129</bib_number> 
<bib_pages>1739-44</bib_pages> 
<bib_year>1993</bib_year> 
<bib_extra key="pmid">8397445</bib_extra> 

부분적으로 내가 원하는 일에 나는 다음과 같은 나오지도 명령을 사용할 수 있습니다

있지만, URL 인코딩 "% 20"이 (가) 유지됩니다. bibtex 태그에서만 어떻게 제거 할 수 있습니까?

sed -e 's/\(\ \ \ \ <filename>file:\/\/\/home\/dwickrama\/Desktop\/stevenJonesLab\/papers\)\([^.]*\)\(\.\?\)\(.*\)\(<\/filename>\)/\1\2\3\4\5\n\ \ \ \ <bib_extra\ key=\"File\">article:\.\.\2\3\4:\4<\/bib_extra>/g' NewPapers.reflib > NewPapers.new.reflib 

답변

1

Regex와 sed는 XML 또는 URL 디코딩을 처리하는 데는별로 좋지 않은 툴입니다.

보다 완벽한 스크립트 언어의 빠른 스크립트를 사용하면보다 명확하고 신뢰할 수 있습니다. 파이썬에서 예를 들면 다음과 같습니다.

import urllib, urlparse 
from xml.dom import minidom 

doc= minidom.parse('NewPapers.reflib') 
el= doc.getElementsByTagName('filename')[0] 
path= urlparse.urlparse(el.firstChild.data)[2] 
foldername, filename= map(urllib.unquote, path.split('/')[-2:]) 

extra= doc.createElement('bib_extra') 
extra.setAttribute('key', 'File') 
extra.appendChild(document.createTextNode('article:../%s/%s:pdf' % (foldername, filename))) 
el.parentNode.insertBefore(extra, el.nextSibling) 
doc.writexml(open('NewPapers.new.reflib')) 

는 (나는 그것이 가장 간단한 방법은 filename= filename.replace(' ', '\\ ') 될 것입니다 형식 정확히 분명 아니다로 백 슬래시 이스케이프 주어진 예제 출력을 재현하는 기능을 포함,하지만하지 않은 그게 맞을지 모르겠다.)

+0

이것은 내 것보다 훨씬 나은 해결책입니다. 감사합니다. –

+0

그런데 명령어 filename = filename.replace ('', '\\') 정확히 내가 필요한 것입니다. 나는 파일 이름 안에서 공백만을 이스케이프 처리한다. –

+0

백 슬래시 자체는 무엇입니까? '.replace ('\\', '\\\\')'? – bobince

0

당신이 필요한 것은 오른쪽 뒤에 줄을 추가하는 것입니다. 그래서 검색 한 후 인쇄하십시오.

#!/bin/bash 

s='<bib_extra key="File">article:../Transcription\\ Factor\\ Binding/A\\ Common\\ Nuclear\\ Signal\\ Transduction\\ Pathway\\ Activated\\ by\\ Growth\\ Factor\\ and\\ Cytokine.pdf:pdf</bib_extra>' 

awk -vstr="$s" ' 
/<filename>/{ 
    print 
    print str;next 
} 
{print}' file 
+0

답장을 보내 주셔서 감사합니다. 큰 xml 파일이 있습니다.이 특정 파일 이름뿐만 아니라 모든 파일 이름을 복제하고 수정하고 싶습니다. 플러스 귀하의 예를 적절하게 라인을 수정하지 않습니다. –