19
20

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でできる設定ファイル書き換えメモ

Posted at

よくフレームワークとかでDBの設定項目とか使ってる環境に変更することがある時にワザワザ手動で書き換える面倒なのでshellで自動置換できるようにした時のメモ

php編

配列の場合

例にこんな設定項目があったとする

sample.php
array(
	'connection'  => array(
		'hostname' => 'localhost',
		'port' => '3306',
		'database' => 'hoge',
		'username'   => 'username',
		'password' => 'password',
	),
),

hostnameを書き換えたい場合はこんな感じでできる

sed -i -e "s/'hostname' => \('.*'\)/'hostname' => 'hoge'/g" test.php 

localhostがhogeに変更できる

同様にport,database,username,passwordも変更してみよう(sedなら-eの連続で渡せる)

sed -i -e "s/'hostname' => \('.*'\)/'hostname' => 'hoge'/g" -e "s/'port' => \('.*'\)/'port' => '3312'/g" -e "s/'database' => \('.*'\)/'database' => 'myproject'/g" -e "s/'username' => \('.*'\)/'username' => 'myuser'/g" -e "s/'password' => \('.*'\)/'passward' => 'mypass'/g" sample.php 

書き換えたファイル

sample.php
array(
	'connection'  => array(
		'hostname' => 'hoge',
		'port' => '3312',
		'database' => 'myproject',
		'username'   => 'username',
		'passward' => 'mypass',
		'presistent' => false
	),
),

ダブルコーテションの場合もちょっと書き換えるだけでいける

sample.php
		"connection"  => array(
			"hostname" => "localhost"
			"port" => "3307",
		),
sed -i -e 's/"hostname" => \(".*"\)/"hostname" => "hoge"/g' sample.php 

'(シングルコーテション)をダブルコーテションに書き換えるだけ
※-eのオプションは逆にシングルコーテーションで囲む

sample.php
		"connection"  => array(
			"hostname" => "fuga"
			"port" => "3307",
		),

ini編

これはシンプルにいける

sampleini.ini
;sample code
[db]
hostname = localhost
port = 3306
database = hgoe
username = username
password = password
sed -i -e "s/hostname = \(.*\)/hostname = fuga/g"  sampleini.php 
sampleini.ini
;sample code
[db]
hostname = fuga
port = 3306
database = hgoe
username = username
password = password

json編

例にこんな設定項目があったとする

sample.json
   "db": {
        "hostname" : "localhost"
        "port": "3307",
        "username": "username",
        "password": "password"
    }

hostnameを書き換えるならこれ

sed -i -e 's/"hostname" : \(.*\)/"hostname" : "fuga"/g' test4.json 
sample.json
   "db": {
        "hostname" : "fuga"
        "port": "3307",
        "username": "username",
        "password": "password"
    }

portやusernameとかも配列の例と同様に-eで繋げればか書き換えられます

応用編 同じ項目が複数ある場合は?

行数の範囲指定をすればいける

sample.json
  1     "db": {
  2         "hostname" : "localhost"
  3         "port": "3307",
  4         "username": "username",
  5         "password": "password"
  6     },
  7     "db2":{
  8         "hostname" : "localhost"
  9         "port": "3307",
 10         "username": "username",
 11         "password": "password"
 12     }
sed -i -e '1,6s/"hostname" : \(.*\)/"hostname" : "fuga"/g' test4.json 
sample.json
  1     "db": {
  2         "hostname" : "fuga"
  3         "port": "3307",
  4         "username": "username",
  5         "password": "password"
  6     },
  7     "db2":{
  8         "hostname" : "localhost"
  9         "port": "3307",
 10         "username": "username",
 11         "password": "password"
 12     }

設定変更shellscriptとか書くときに役に立つとおもいます

19
20
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
19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?