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 2016-05-13

sed で条件にマッチする行の行頭に文字を入れる

例) 文字列'hoge'が含まれる行の頭に文字列'foo'を入れる

 $ cat txt
 aaaaaaaaaa
 bbbhogebbb
 cccccccccc
 dddhogeddd
 
 $ sed -i -e "s/.*hoge.*/foo&/" txt
 
 $ cat txt
 aaaaaaaaaa
 foobbbhogebbb
 cccccccccc
 foodddhogeddd

ちなみに '&'1 はマッチした文字列という意味である。この例では'hoge'が含まれる行全体にマッチするから、すなわち'hoge'が含まれる行の頭にfooをつけて置換するという処理になる。

  1. GNU版のsedコマンドでは '&' の代わりに '\0' と書いてもよい。

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?