Pubmedの結果をviで整形する(不要な改行の削除)

Pubmedはとても便利ですが、いくつか不便なこともあります。
そのうちのひとつに、summary (text) の扱いです。
viを使ってなんとかきれいにできないかと試行錯誤した結果です。

先に結論を示すと、以下でできます。

:%s/\n\([A-Za-z0-9]\)/ \1/

以下がそのプロセスです。

具体例を示した方がいいので、以下に示します。
統合失調症のVBMに関して知りたかったので、PubmedでSchizophrenia VBMで検索しました。
最初に出てきた5本の論文をPubmedでSummary(text)で表示した結果を以下に示します。

1: Salvador R, Radua J, Canales-Rodríguez EJ, Solanes A, Sarró S, Goikolea JM,
Valiente A, Monté GC, Natividad MDC, Guerrero-Pedraza A, Moro N,
Fernández-Corcuera P, Amann BL, Maristany T, Vieta E, McKenna PJ, Pomarol-Clotet 
E. Evaluation of machine learning algorithms and structural features for optimal 
MRI-based diagnostic prediction in psychosis. PLoS One. 2017 Apr
20;12(4):e0175683. doi: 10.1371/journal.pone.0175683. eCollection 2017. PubMed
PMID: 28426817.


2: Li Y, Li WX, Xie DJ, Wang Y, Cheung EF, Chan RC. Grey matter reduction in the 
caudate nucleus in patients with persistent negative symptoms: An ALE
meta-analysis. Schizophr Res. 2017 Apr 5. pii: S0920-9964(17)30192-5. doi:
10.1016/j.schres.2017.04.005. [Epub ahead of print] Review. PubMed PMID:
28390850.


3: Van der Auwera S, Wittfeld K, Shumskaya E, Bralten J, Zwiers MP, Onnink AM,
Usberti N, Hertel J, Völzke H, Völker U, Hosten N, Franke B, Grabe HJ. Predicting
brain structure in population-based samples with biologically informed genetic
scores for schizophrenia. Am J Med Genet B Neuropsychiatr Genet. 2017
Apr;174(3):324-332. doi: 10.1002/ajmg.b.32519. PubMed PMID: 28304149.


4: Stip E, Cherbal A, Luck D, Zhornitsky S, Bentaleb LA, Lungu O. A neuroimaging 
study of emotion-cognition interaction in schizophrenia: the effect of
ziprasidone treatment. Psychopharmacology (Berl). 2017 Apr;234(7):1045-1058. doi:
10.1007/s00213-017-4533-9. Epub 2017 Feb 17. PubMed PMID: 28210783.


5: Pang L, Kennedy D, Wei Q, Lv L, Gao J, Li H, Quan M, Li X, Yang Y, Fan X, Song
X. Decreased Functional Connectivity of Insular Cortex in Drug Naïve First
Episode Schizophrenia: In Relation to Symptom Severity. PLoS One. 2017 Jan
20;12(1):e0167242. doi: 10.1371/journal.pone.0167242. eCollection 2017. PubMed
PMID: 28107346; PubMed Central PMCID: PMC5249106.

左の行番号を見ればわかりますが、一行一行で改行がかかってしまっています。
これをWordなどに貼り付けると、その都度改行を削除しなければいけません。

これは非常に効率が悪いです。

そこで、改行をviでスマートに削除したいと考えました。

まず、シンプルに改行を削除するコマンドを考えました。(おそらくダメですが)
改行は、\n で表わせ、行末は $ で表わせますから、

:%s/$\n//

で行末の改行が削除できます。

やってみた結果を示します。

1: Salvador R, Radua J, Canales-Rodríguez EJ, Solanes A, Sarró S, Goikolea J    M,Valiente A, Monté GC, Natividad MDC, Guerrero-Pedraza A, Moro N,Fernández-    Corcuera P, Amann BL, Maristany T, Vieta E, McKenna PJ, Pomarol-Clotet E. Ev    aluation of machine learning algorithms and structural features for optimal     MRI-based diagnostic prediction in psychosis. PLoS One. 2017 Apr20;12(4):e01    75683. doi: 10.1371/journal.pone.0175683. eCollection 2017. PubMedPMID: 2842    6817.2: Li Y, Li WX, Xie DJ, Wang Y, Cheung EF, Chan RC. Grey matter reducti    on in the caudate nucleus in patients with persistent negative symptoms: An     ALEmeta-analysis. Schizophr Res. 2017 Apr 5. pii: S0920-9964(17)30192-5. doi    :10.1016/j.schres.2017.04.005. [Epub ahead of print] Review. PubMed PMID:283    90850.3: Van der Auwera S, Wittfeld K, Shumskaya E, Bralten J, Zwiers MP, On    nink AM,Usberti N, Hertel J, Völzke H, Völker U, Hosten N, Franke B, Grabe H    J. Predictingbrain structure in population-based samples with biologically i    nformed geneticscores for schizophrenia. Am J Med Genet B Neuropsychiatr Gen    et. 2017Apr;174(3):324-332. doi: 10.1002/ajmg.b.32519. PubMed PMID: 28304149    .4: Stip E, Cherbal A, Luck D, Zhornitsky S, Bentaleb LA, Lungu O. A neuroim    aging study of emotion-cognition interaction in schizophrenia: the effect of    ziprasidone treatment. Psychopharmacology (Berl). 2017 Apr;234(7):1045-1058.     doi:10.1007/s00213-017-4533-9. Epub 2017 Feb 17. PubMed PMID: 28210783.5: P    ang L, Kennedy D, Wei Q, Lv L, Gao J, Li H, Quan M, Li X, Yang Y, Fan X, Son    gX. Decreased Functional Connectivity of Insular Cortex in Drug Naïve FirstE    pisode Schizophrenia: In Relation to Symptom Severity. PLoS One. 2017 Jan20;    12(1):e0167242. doi: 10.1371/journal.pone.0167242. eCollection 2017. PubMedPMID: 28107346; PubMed Central PMCID: PMC5249106.

これでは、全部がつながって1行になってしまいました。

あわてて u で元に戻します。

もう少し深く考えると、

  • 次の行に文字がある場合の改行は削除したい
  • 削除だけだと文字がつながってしまうため、スペースを入れたい

わけです。

つまり、改行+何か一文字を検索すればいいわけです。

ここで、以下を考えました。

:%s/\n[A-Za-z0-9]/ /

これで、改行のあとに文字がある場合に改行をスペースで置換できるはずです。

1: Salvador R, Radua J, Canales-Rodríguez EJ, Solanes A, Sarró S, Goikolea J    M, aliente A, Monté GC, Natividad MDC, Guerrero-Pedraza A, Moro N, ernández-    Corcuera P, Amann BL, Maristany T, Vieta E, McKenna PJ, Pomarol-Clotet  . Ev    aluation of machine learning algorithms and structural features for optimal      RI-based diagnostic prediction in psychosis. PLoS One. 2017 Apr 0;12(4):e01    75683. doi: 10.1371/journal.pone.0175683. eCollection 2017. PubMed MID: 2842    6817.

 : Li Y, Li WX, Xie DJ, Wang Y, Cheung EF, Chan RC. Grey matter reduction in     the  audate nucleus in patients with persistent negative symptoms: An ALE e    ta-analysis. Schizophr Res. 2017 Apr 5. pii: S0920-9964(17)30192-5. doi: 0.1    016/j.schres.2017.04.005. [Epub ahead of print] Review. PubMed PMID: 8390850    .

 : Van der Auwera S, Wittfeld K, Shumskaya E, Bralten J, Zwiers MP, Onnink A    M, sberti N, Hertel J, Völzke H, Völker U, Hosten N, Franke B, Grabe HJ. Pre    dicting rain structure in population-based samples with biologically informe    d genetic cores for schizophrenia. Am J Med Genet B Neuropsychiatr Genet. 20    17 pr;174(3):324-332. doi: 10.1002/ajmg.b.32519. PubMed PMID: 28304149.

 : Stip E, Cherbal A, Luck D, Zhornitsky S, Bentaleb LA, Lungu O. A neuroima    ging  tudy of emotion-cognition interaction in schizophrenia: the effect of     iprasidone treatment. Psychopharmacology (Berl). 2017 Apr;234(7):1045-1058.     doi: 0.1007/s00213-017-4533-9. Epub 2017 Feb 17. PubMed PMID: 28210783.

 : Pang L, Kennedy D, Wei Q, Lv L, Gao J, Li H, Quan M, Li X, Yang Y, Fan X,     Song . Decreased Functional Connectivity of Insular Cortex in Drug Naïve Fi    rst pisode Schizophrenia: In Relation to Symptom Severity. PLoS One. 2017 Ja    n 0;12(1):e0167242. doi: 10.1371/journal.pone.0167242. eCollection 2017. Pub    Med MID: 28107346; PubMed Central PMCID: PMC5249106.

ここで私のおっちょこちょいに気づきました。あ、改行の次の文字もまとめてスペースに置換されている…と。

でも、求めたいところには近づいています。あとは、改行文字の次の文字を残しさえすればいいわけです。
Viでどうすればいいのか調べると、検索文字の再利用という方法があることを知りました。

Viでは、検索文字を( ) でくくり、その後、\1, \2として使うと再利用できるのだそうです。

今、再利用するのはひとつだけ、改行文字の後の一文字だけです。

三度目の正直で、以下を行いました。

:%s/\n\([A-Za-z0-9]\)/ \1/
1: Salvador R, Radua J, Canales-Rodríguez EJ, Solanes A, Sarró S, Goikolea JM, Valiente A, Monté GC, Natividad MDC, Guerrero-Pedraza A, Moro N, Fernández-Corcuera P, Amann BL, Maristany T, Vieta E, McKenna PJ, Pomarol-Clotet  E. Evaluation of machine learning algorithms and structural features for optimal  MRI-based diagnostic prediction in psychosis. PLoS One. 2017 Apr 20;12(4):e0175683. doi: 10.1371/journal.pone.0175683. eCollection 2017. PubMed PMID: 28426817.

 2: Li Y, Li WX, Xie DJ, Wang Y, Cheung EF, Chan RC. Grey matter reduction in the  caudate nucleus in patients with persistent negative symptoms: An ALE meta-analysis. Schizophr Res. 2017 Apr 5. pii: S0920-9964(17)30192-5. doi: 10.1016/j.schres.2017.04.005. [Epub ahead of print] Review. PubMed PMID: 28390850.

 3: Van der Auwera S, Wittfeld K, Shumskaya E, Bralten J, Zwiers MP, Onnink AM, Usberti N, Hertel J, Völzke H, Völker U, Hosten N, Franke B, Grabe HJ. Predicting brain structure in population-based samples with biologically informed genetic scores for schizophrenia. Am J Med Genet B Neuropsychiatr Genet. 2017 Apr;174(3):324-332. doi: 10.1002/ajmg.b.32519. PubMed PMID: 28304149.

 4: Stip E, Cherbal A, Luck D, Zhornitsky S, Bentaleb LA, Lungu O. A neuroimaging  study of emotion-cognition interaction in schizophrenia: the effect of ziprasidone treatment. Psychopharmacology (Berl). 2017 Apr;234(7):1045-1058. doi: 10.1007/s00213-017-4533-9. Epub 2017 Feb 17. PubMed PMID: 28210783.

 5: Pang L, Kennedy D, Wei Q, Lv L, Gao J, Li H, Quan M, Li X, Yang Y, Fan X, Song X. Decreased Functional Connectivity of Insular Cortex in Drug Naïve First Episode Schizophrenia: In Relation to Symptom Severity. PLoS One. 2017 Jan 20;12(1):e0167242. doi: 10.1371/journal.pone.0167242. eCollection 2017. PubMed PMID: 28107346; PubMed Central PMCID: PMC5249106.

文献番号の前にスペースが入ってしまいましたが、それ以外はきれいに1行になっているのがわかります。また、文献と文献の間は2行改行が入っているので、この作業によって1行になり、そこもすっきりしています。

これで、業績などを書く時の手間が大幅に省けるようになりました。

ちなみに、sedでも全く同じ方法でいけます。
publication.txtというテキストファイルだとすると、

cat publication.txt | sed 's/\n\([A-Za-z0-9]\)/ \1/' > pub_formated.txt

で同様の結果を得ることができます。

Print Friendly, PDF & Email

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください