
以下にサンプルファイルがあります:
# This is a test file. This is a test file. This is a test file
This is a test file. This is a test file. This is a test file.
This is a test file.
# Need to output just this paragraph.Need to output just this paragraph.
Need to output just this paragraph TOO. Need to output just this paragraph.
Need to output just this paragraph.
「#」から始まる2番目の段落から段落の最後の文までだけを出力する必要があります。
パターンに基づいて grep して出力するにはどうすればよいですか? ファイルに複数の段落があり、「TOO」という単語を含む段落を出力したいとします。
答え1
段落が空行で区切られている場合:
awk -v RS= -v ORS='\n\n' /TOO/
空のレコードセパレータ(RS
)は段落モードレコードは空行のシーケンスによって区切られます。
分離されている場合#
:
awk -v RS='#' '/TOO/ {print RS $0}'
または
pcregrep -Mo '#[^#]*?TOO[^#]*'
-M
複数行の場合grep
-o
一致した部分のみを出力する
答え2
perl -00ne 'print if /TOO/'
-00
段落モードを意味します (レコードは 1 行以上の空行で区切られます)。