sedの勧め

私のように環境構築事態をシェルで行うものにとっては欠かせない道具
それがsedです。かなり縁の下の力持ち的な存在ですが、
設定ファイルを書き換えてくれる機能はやはり環境構築には欠かせません。

いつも使ってる環境構築用シェルからsed部分を紹介します。
どれか一つでも誰かの役に立てれれば幸いです。

# Mac でVagrantfileの編集するとき
```
sed -i -e "s/.*192.168.33.10.*/  config.vm.network \"private_network\", ip: \"192.168.33.10\"/" Vagrantfile

sed -i -e 's/# config.vm.network "forwarded_port", guest: 80, host: 8080/config.vm.network "forwarded_port", guest: 80, host: 8080/g' Vagrantfile
```

# /etc/sudoersの設定
置換パターン
コメントアウトとコメントイン、コメントインと置換

```
sed -i 's/Defaults.*requiretty/#Default\trequiretty/g' /etc/sudoers
sed -i 's/^#\s%wheel\s*ALL=(ALL)\s*ALL$/%wheel\tALL=(ALL)\tALL/g' /etc/sudoers
sed -i 's/^#\s%wheel\s*ALL=(ALL)\s*NOPASSWD: ALL$/vagrant\tALL=(ALL)\tNOPASSWD: ALL/g' /etc/sudoers
```

# /etc/resolv.confの設定
行指定で追加パターン

```
sed -i -e "1i options single-request-reopen" /etc/resolv.conf
```

# 時刻変更
置換パターン

```
sed -i "s/UTC/Asia\/Tokyo/g" /etc/sysconfig/clock
```


# SELinux無効設定
置換パターン

```
sed -i "s/\(^SELINUX=\).*/\1disabled/" /etc/selinux/config
```

# /etc/my.cnfの設定
行指定で追加パターン

```
sed -i -e "28i character_set_server=utf8" /etc/my.cnf
sed -i -e "29i # エラーログの確認" /etc/my.cnf
sed -i -e "30i general_log=1" /etc/my.cnf
sed -i -e "31i general_log_file=/var/log/mysql/query.log" /etc/my.cnf
sed -i -e "32i # slowクエリログを表示" /etc/my.cnf
sed -i -e "33i slow_query_log = 1" /etc/my.cnf
sed -i -e "34i long_query_time = 2" /etc/my.cnf
sed -i -e "35i slow_query_log_file = /var/log/mysql/slow.log" /etc/my.cnf
sed -i -e "36i [client]" /etc/my.cnf
sed -i -e "37i default-character-set = utf8" /etc/my.cnf
sed -i -e "38i [mysqldump]" /etc/my.cnf
sed -i -e "39i default-character-set = utf8" /etc/my.cnf
sed -i -e "40i [mysql]" /etc/my.cnf
sed -i -e "41i default-character-set = utf8" /etc/my.cnf
```

# /etc/php.iniの設定
置換パターン

```
sed -i -e "s|;error_log = syslog|error_log = /var/log/php.log|" /etc/php.ini
sed -i -e "s|;mbstring.language = Japanese|mbstring.language = Japanese|" /etc/php.ini
sed -i -e "s|;mbstring.internal_encoding = EUC-JP|mbstring.internal_encoding = UTF-8|" /etc/php.ini
sed -i -e "s|;mbstring.http_input = auto|mbstring.http_input = auto|" /etc/php.ini
sed -i -e "s|;mbstring.detect_order = auto|mbstring.detect_order = auto|" /etc/php.ini
sed -i -e "s|expose_php = On|expose_php = Off|" /etc/php.ini
sed -i -e "s|;date.timezone =|date.timezone = Asia/Tokyo|" /etc/php.ini
```

# /etc/httpd/conf/httpd.confの設定

置換パターン

```
sed -i    "/^#NameVirtualHost \*:80$/ s/#NameVirtualHost \*:80/NameVirtualHost \*:80/" /etc/httpd/conf/httpd.conf
sed -i -e "s/KeepAlive Off/KeepAlive On/g" /etc/httpd/conf/httpd.conf
sed -i -e "s/AllowOverride None/AllowOverride All/g" /etc/httpd/conf/httpd.conf
sed -i -e "s/DirectoryIndex index.html index.html.var/DirectoryIndex index.html index.php/g" /etc/httpd/conf/httpd.conf
```

最終行に追加パターン

```
sed -i '$a\<VirtualHost *:80>' /etc/httpd/conf/httpd.conf
sed -i '$a\DocumentRoot /home/vagrant/fuelphp/public/' /etc/httpd/conf/httpd.conf
sed -i '$a\<Directory /home/vagrant/fuelphp/public/>' /etc/httpd/conf/httpd.conf
sed -i '$a\DirectoryIndex index.php' /etc/httpd/conf/httpd.conf
sed -i '$a\AllowOverride All' /etc/httpd/conf/httpd.conf
sed -i '$a\Order allow,deny' /etc/httpd/conf/httpd.conf
sed -i '$a\Allow from all' /etc/httpd/conf/httpd.conf
sed -i '$a\EnableMMAP Off' /etc/httpd/conf/httpd.conf
sed -i '$a\EnableSendfile Off' /etc/httpd/conf/httpd.conf
sed -i '$a\</Directory>' /etc/httpd/conf/httpd.conf
sed -i '$a\</VirtualHost>' /etc/httpd/conf/httpd.conf
```