0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sed 正規表現で置換メモ

Last updated at Posted at 2019-05-27

sed で正規表現を使ってファイルを編集する

いつも忘れるのでメモ。

 sed -i<backup_ext> -r -e s/<regexp_pattern>/<replacement>/ <file_path>
スイッチ 意味
-i ファイルを直接編集。(スペースをつけずに)後ろに文字列を追加するとこの値でバックアップファイルが作成される
-r 正規表現を使う
-e コマンド、もしくはスクリプトの実行

例)

こんなファイルを用意

% cat input.txt 
abcdaaa
abcdaba
aaabcde
% sed -i.back -r -e s/aaa$/bbb/ input.txt

とすると

% cat input.txt
abcdbbb
abcdaba
aaabcde

% cat input.txt.back 
abcdaaa
abcdaba
aaabcde

のようになる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?