検索してこれら2つのトピックを見つけましたが、スペースの数が固定されているため、異なります。一方、私のサンプルには空白の数が固定されていません。
https://stackoverflow.com/questions/47428445/i-want-grep-to-grep-one-word-which-is-having-spaces-it
https://askubuntu.com/questions/949326/how-to-include-a-space-character-with-grep
テキスト例:
<span>Section 1: Plan your day, write out your plan</span>
希望の出力:
Section 1: Plan your day, write out your plan
HTMLタグではなくテキストだけをgrepしたいです。これが私の試みです。
wolf@linux:~$ cat file.txt
<span>Section 1: Plan your day, write out your plan</span>
wolf@linux:~$
wolf@linux:~$ grep -oP 'S\S+ \d: \S+' file.txt
Section 1: Plan
wolf@linux:~$
wolf@linux:~$ grep -oP 'S\S+ \d: \S+ \S+' file.txt
Section 1: Plan your
wolf@linux:~$
\S+
テキストの長さが異なるため、1つずつ定義するよりも優れた解決策がありますか?
ベストアンサー1
拡張正規表現を使用してSection
キーワードを固定し、その後に来ないすべての項目を取得します<
。
$ grep -E -o 'Section [0-9]+:[^<]*' < file.txt
Section 1: Plan your day, write out your plan
Perlを使用して周辺部分を固定するのが最も簡単な方法なので、これがオプションの場合:
$ perl -lne 'print $1 if m,<span>(Section \d+:.*?)</span>,' < file.txt
Section 1: Plan your day, write out your plan
(同様の操作を実行するために使用できるいくつかの方法がありますが、grep -P
読みにくいです。)