特定の文字列から始まるテキストの削除

特定の文字列から始まるテキストの削除

たとえば、Linuxシステム上のファイルに次のテキストがある場合:

10-02-2020
given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines.
In addition, two variant programs egrep and fgrep are available. egrep is the same as grep -E. fgrep is the same as grep -F
16-02-2020
The top program provides a dynamic real-time view of a running
       system.  It can display system summary information as well as a list
       of processes or threads currently being managed by the Linux kernel.
       The types of system summary information shown and the types, order
       and size of information displayed for processes are all user
       configurable and that configuration can be made persistent across
       restarts.

16-02-2020前のテキストをすべて削除してファイルを次のように置き換えるにはどうすればよいですか?

16-02-2020
The top program provides a dynamic real-time view of a running
       system.  It can display system summary information as well as a list
       of processes or threads currently being managed by the Linux kernel.
       The types of system summary information shown and the types, order
       and size of information displayed for processes are all user
       configurable and that configuration can be made persistent across
       restarts.

ベストアンサー1

を使用してこれを行うことができますsed。一般的な形式は次のとおりです。

sed -n '/pattern1/,/pattern2/p' file

明示的な指示がない限り、理由は印刷されません-n。 Somこのコマンドは、行一致と1行一致(含む)の間のすべての行を印刷します。一致する項目が複数ある場合、複数行が印刷されます。sedppattern1pattern2

あなたの場合は、ファイルの最後まですべての内容を印刷したいので、pattern2次の$ものを探しています。

$ sed -n '/16-02-2020/,$p' file
16-02-2020
The top program provides a dynamic real-time view of a running
   system.  It can display system summary information as well as a list
   of processes or threads currently being managed by the Linux kernel.
   The types of system summary information shown and the types, order
   and size of information displayed for processes are all user
   configurable and that configuration can be made persistent across
   restarts.

fgrep関連性がなく、使用されなくなったコメントでは、およびを使用する必要がegrepあります。バラより:grep -Fgrep -Eman grep

また、変形プログラムegrepおよびfgrepは、それぞれgrep-Eおよびgrep-Fに等しい。これらのバリエーションは廃止予定ですが、以前のバージョンとの互換性のために提供されています。

おすすめ記事