grepおよび/またはsedを使用してxmlファイルから複数行のテキストを抽出する方法

grepおよび/またはsedを使用してxmlファイルから複数行のテキストを抽出する方法

目的のテキストの<w:t>前と末尾の一部の行を抽出しようとしますが、</w:t>最後のタグのテキストのみをインポートし、他のタグはインポートしません。どうすればいいですか?

これは私が使用しようとしたコードです:

grep '<w:t>' word/document.xml | sed 's/.*<w:t>\(.*)<\/w:t>.*/\1/' | cat > brev.txt

ご覧のように、単語ディレクトリにあるファイルgrepからdocument.xmlファイルのタグを見つけてというファイルに転送していますが、brev.txt正しく機能しません。ラベル付きの最後の行だけでなく、すべての行を取得するにはどうすればよいですか?

ファイルdocument.xmlは1行のテキストファイルです(違いがある場合)。

また、別のコードを試しましたが、最初の<w:t>タグから最後のタグまで</w:t>すべてが提供されました。ここには追加のテキストがたくさんあります。次のコードは次のとおりです。

grep -o '<w:t>.*</w:t>' word/document.xml | sed 's/\(<w:t>\|<\/w:t>\)//g' > brev.txt

サンプルファイル(読みやすくするためにフォーマットされている、元のファイルは1行)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
  <w:body>
    <w:p w14:paraId="35B527D8" w14:textId="4CF0BDCB" w:rsidR="0068138C" w:rsidRDefault="00BF1E48">
      <w:r>
        <w:t>Here’s a Word document. It has several sentences.</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="4AADFADF" w14:textId="4F49E2CE" w:rsidR="00BF1E48" w:rsidRDefault="00BF1E48">
      <w:r>
        <w:t>Most are short.</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="608ED30C" w14:textId="2163C420" w:rsidR="00BF1E48" w:rsidRDefault="00BF1E48">
      <w:r>
        <w:t>All are in English.</w:t>
      </w:r>
    </w:p>
    <w:p w14:paraId="0B67C683" w14:textId="77777777" w:rsidR="00BF1E48" w:rsidRDefault="00BF1E48">
      <w:bookmarkStart w:id="0" w:name="_GoBack"/>
      <w:bookmarkEnd w:id="0"/>
    </w:p>
    <w:sectPr w:rsidR="00BF1E48">
      <w:pgSz w:w="11906" w:h="16838"/>
      <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
      <w:cols w:space="708"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>

ベストアンサー1

XMLパーサーを使用してXMLを解析します。あなたの質問に追加したサンプル文書を使用して、

xmlstarlet sel -t -v '//w:t' -n word/document.xml >brev.txt

cat brev.txt
Here’s a Word document. It has several sentences.
Most are short.
All are in English.

XMLパーサーは実際には使用できませんが、GNUがある場合はgrepこのモードを使用できます。しかし、これは問題を解決する誤った方法です。

grep -oP '(?<=<w:t>).*?(?=</w:t>)' word/document.xml

おすすめ記事