行末までの正規表現の後のテキストを削除します。

行末までの正規表現の後のテキストを削除します。

このようなファイルがあります。

this is a year (2004); this text is not insteresting
singer elton john; month and year (December, 2005); blah blah
this another year (2007); irrelevant text

あけましておめでとうございます。

this is a year (2004);
singer elton john; month and year (December, 2005);
this another year (2007);

これはうまくいきません

sed -E 's/\(.*\)[0-9]{4});\(.*\)/\2/' file

sedまたはawkを使用してこれをどのように実行できますか?

ベストアンサー1

自分が欲しいものを書き込む効果的な方法は次のとおりです。

sed -E 's/(.*[0-9]{4}\);).*/\1/' file

yyyy);これにより、各行の最後の項目以降のすべての行文字が削除されます。

あなたの試みは

sed -E 's/\(.*\)[0-9]{4});\(.*\)/\2/' file

ただし、-E拡張正規表現フラグが有効なため、一致\( \)グループは分離されませんが、ファイルのリテラル角かっこは一致し、一致( )グループは分離されます。したがって、括弧が一致せず、[0-9]{4})sedが文句を言います。

sed: -e expression #1, char 28: Unmatched ) or \)

おすすめ記事