
私はHTMLファイルのバンドル中にタイトルに「エージェント」という言葉があり、そのタイトルの後に特定のエージェントの名前を持つファイルを見つけようとしています。
それでは普通です。
<h3>Agent</h3>
<p>Blah blah blah </p>
<p>Their agent is XYZ Corp.</p>
見つけることができるはずです
ただし、タイトルとXYZ Corpインスタンスの間のマークアップやコンテンツの規則性は保証できません。したがって、DOSまたは同様の状況では、「Agent * XYZ」の意味を検索できます。
-match the string 'Agent'
-followed by anything
-followed by the string 'XYZ'
Ubuntuでgrepを使ってどのように書くのですか?頑張りました
grep -lc 'Agent*XYZ' *.html
grep -lc 'Agent.*?XYZ' *.html
誰も成功しませんでした。複数のファイルでパターンを手動で見つけることができ、パターンが存在することがわかります。
ティア
答え1
次のようなものが目標に良いようです。
$ cat d2.txt
<h3>Agent</h3>
<p>Blah blah blah </p>
<p>Their agent is XYZ Corp.</p>
$ grep -i 'agent' d2.txt #-i = ignore case. By default grep returns lines containing agent followed by anything or even alone
<h3>Agent</h3>
<p>Their agent is XYZ Corp.</p>
$ grep -iE 'agent.*XYZ' d2.txt #match agent followed by XYZ
<p>Their agent is XYZ Corp.</p>
答え2
h3
タイトルが常にエージェント名とは別の行に表示されると仮定すると、sed
必要に応じて機能するようです。
与えられた入力ファイル
some data
at the top
<h3>Agent</h3>
<p>Blah blah blah </p>
<p>Their agent is XYZ Corp.</p>
some data
at the bottom
注文する
sed -n '\#<h3>Agent</h3>#,/XYZ/p' input.html
生成する
<h3>Agent</h3>
<p>Blah blah blah </p>
<p>Their agent is XYZ Corp.</p>
このコマンドは、sed
2つの正規表現<h3>Agent</h3>
と(含む)とXYZ
一致する行の間のすべての内容を出力します。最初の正規表現を区切る方法は、カスタム区切り\#...#
文字を使用する方法です。私がしていることは脱出モードではありません/
。