Help us understand the problem. What is going on with this article?

sedでできる設定ファイル書き換えメモ

More than 5 years have passed since last update.

よくフレームワークとかで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とか書くときに役に立つとおもいます

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away