LoginSignup
0
0

More than 1 year has passed since last update.

【Linux】特定の文字列以降を削除する方法

Posted at

はじめに

以下のファイルから「ここまで」という文字列までの内容を残しそれより後の文字列は削除する方法について記載します。

sample.txt
あああああ
いいいいい
ううううう
えええええ
おおおおお
ここまで
aaaaa
bbbbb
ccccc
ddddd
eeeee

削除方法

sedコマンドを使用して次のように実行することで特定の文字列より後の行を削除できます。

sed -i "/文字列/q" ファイルパス
$ sed -i "/ここまで/q" sample.txt 
$ cat sample.txt
あああああ
いいいいい
ううううう
えええええ
おおおおお
ここまで

なお「ここまで」も含めて削除する場合には以下のように実行します。

sed -i '/文字列/,$d' ファイルパス
$ sed -i '/ここまで/,$d' sample.txt
$ cat sample.txt
あああああ
いいいいい
ううううう
えええええ
おおおおお
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0