استبدال فقط داخل علامة XML ؛ تصدير من المرجع. REFLIB إلى تنسيق bibtex مع أسماء الملفات سليمة وإزالة عنوان URL ، مع أمر bash

StackOverflow https://stackoverflow.com/questions/2105385

سؤال

لدي العديد من المراجع في المرجع. أحاول تضمين أسماء الملفات في ملف bibtex الخاص بي عند التصدير من المرجع. نظرًا لأن البرنامج لا يقوم بذلك افتراضيًا ، فأنا أحاول استخدام أمر SED لتضمين اسم الملف كمعلومات bibtex في ملف XML قبل التصدير وبالتالي تضمين اسم الملف.

مدخل

  <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>

يمكنني استخدام أمر SED التالي للقيام جزئيًا بما أريد ، لكن عنوان 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
هل كانت مفيدة؟

المحلول

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(' ', '\\ '), ، لكنني لست متأكدًا من أن هذا سيكون صحيحًا.)

نصائح أخرى

كل ما تحتاجه هو إضافة خط بعد اليمين ؟؟ لذلك فقط طباعته بعد البحث.

#!/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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top