grep Lookaroudを何度も使用できますか?

grep Lookaroudを何度も使用できますか?
<table name="content_analyzer" another-key="id9">
  <type="global" />
</table>
<table name="content_analyzer2" another-key="id12">
  <type="global" />
</table>    
<table name="content_analyzer" primary-key="id9">
  <type="global" />
</table>
<table name="content_analyzer2" primary-key="id12">
  <type="global" />
</table>
<table name="content_analyzer_items" primary-key="id56">
  <type="global" />
</table>

name 値を抽出するには、次のようにします。

grep -Po 'name="\K.*?(?=")'

しかし、名前と主キーの値の両方をどのように処理しますか?たとえば、次の行は機能しません。

grep -Po 'name="\K.*?(?=") primary-key="\K.*?(?=")'

だからそれは

content_analyzer id9
content_analyzer2 id12
content_analyzer_items id56

ベストアンサー1

使用sed:

$ sed -nr 's/^[^\s]+\sname="([^"]+)"\s.*primary-key="([^"]+)">$/\1 \2/p' file.txt
content_analyzer id9
content_analyzer2 id12
content_analyzer_items id56

PCREで使用すると、次のようになりgrepます。

$ grep -Po '(name|primary-key)="\K[^"]+' file.txt                         
content_analyzer
id9
content_analyzer2
id12
content_analyzer_items
id56

おすすめ記事