1、在最后一行加:
1 | sed -i '$a\Defaults logfile=/var/log/sudo.log' /etc/sudoers |
2、替换字符:
1 | sed -i 's/Allow from .example.com/Allow from localhost/g' /usr/local/xxx/config/apache/extra/httpd-info .conf |
3、在匹配行下方几行开始加:
1 2 3 4 5 | sed -i -e '/location ~ \/\\.ht/{n;n;s|$|\n #-------------------------------------------------|}' /usr/local/xxx/config/nginx/vhost/default .conf sed -i -e '/location ~ \/\\.ht/{n;n;n;s|$|\n location /nginx-status {|}' /usr/local/xxx/config/nginx/vhost/default .conf sed -i -e '/location ~ \/\\.ht/{n;n;n;n;s|$|\n stub_status on;|}' /usr/local/xxx/config/nginx/vhost/default .conf sed -i -e '/location ~ \/\\.ht/{n;n;n;n;n;s|$|\n access_log off;|}' /usr/local/xxx/config/nginx/vhost/default .conf sed -i -e '/location ~ \/\\.ht/{n;n;n;n;n;n;s|$|\n }|}' /usr/local/xxx/config/nginx/vhost/default .conf |
4、删除匹配性开头的行:
1 | sed -i '/^Defaults logfile=\/var\/log\/sudo.log.*/d' /etc/sudoers |
5、删除匹配行,并且删除前一行,后三行:
1 | sed -i '/location \/nginx-status/,+3d;:go;1,1!{P;$!N;D};N;bgo' /usr/local/xxx/config/nginx/vhost/default .conf |
6、利用字符串匹配取区间
1 2 3 4 5 6 | 本题可以取以 "device" 开头以 "}" 结尾,然后将里面的符合要求的字符串过滤计数就可以了。 取区间的方法: sed -n '/^device/,/\}$/p' oldboy.log awk '/device: {/, /}/' oldboy.log 提示: sed -n '/区间开始标识/,/区间结束标识/p' oldboy.log |
7、从多少行到多少行
1 | sed -n '16214,24400p' default.err >mysqlerr.log |
8、将\n换行符替换成空格(sed高级应用很重要)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@localhost ~] # chkconfig --list | grep xyz| awk '{print $1}' | sed 's/\/n/ /g' xyz_apache xyz_app xyz_clamd xyz_ctasd_in xyz_ctasd_out xyz_dovecot xyz_dspam xyz_mysqld xyz_nginx xyz_openldap xyz_postfix xyz_postgresql xyz_postgrey xyz_redis xyz_spamassassin 方法1: [root@localhost ~] # chkconfig --list | grep umail| awk '{print $1}' | sed ':a;N;s/\n/ /g;ta' xyz_apache xyz_app xyz_clamd xyz_ctasd_in xyz_ctasd_out xyz_dovecot xyz_dspam xyz_mysqld xyz_nginx xyz_openldap xyz_postfix xyz_postgresql xyz_postgrey xyz_redis xyz_spamassassin 方法2: [root@localhost ~] # chkconfig --list | grep umail| awk '{print $1}' | tr "\n" " " xyz_apache xyz_app xyz_clamd xyz_ctasd_in xyz_ctasd_out xyz_dovecot xyz_dspam xyz_mysqld xyz_nginx xyz_openldap xyz_postfix xyz_postgresql xyz_postgrey xyz_redis xyz_spamassassin 命令扩展: [root@localhost~] # for i in `chkconfig --list | grep xyz| awk '{print $1}' | sed ':a;N;s/\n/ /g;ta'`; do /etc/init.d/$i status; done |