1、在最後一行加:
1
<code>sed</code> <code>-i </code><code>'$a\Defaults logfile=/var/log/sudo.log'</code> <code>/etc/sudoers</code>
2、替換字元:
<code>sed</code> <code>-i </code><code>'s/Allow from .example.com/Allow from localhost/g'</code> <code>/usr/local/xxx/config/apache/extra/httpd-info</code><code>.conf</code>
3、在比對行下方幾行開始加:
2
3
4
5
<code>sed</code> <code>-i -e </code><code>'/location ~ \/\\.ht/{n;n;s|$|\n #-------------------------------------------------|}'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
<code>sed</code> <code>-i -e </code><code>'/location ~ \/\\.ht/{n;n;n;s|$|\n location /nginx-status {|}'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
<code>sed</code> <code>-i -e </code><code>'/location ~ \/\\.ht/{n;n;n;n;s|$|\n stub_status on;|}'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
<code>sed</code> <code>-i -e </code><code>'/location ~ \/\\.ht/{n;n;n;n;n;s|$|\n access_log off;|}'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
<code>sed</code> <code>-i -e </code><code>'/location ~ \/\\.ht/{n;n;n;n;n;n;s|$|\n }|}'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
4、删除比對性開頭的行:
<code>sed</code> <code>-i </code><code>'/^Defaults logfile=\/var\/log\/sudo.log.*/d'</code> <code>/etc/sudoers</code>
5、删除比對行,并且删除前一行,後三行:
<code>sed</code> <code>-i </code><code>'/location \/nginx-status/,+3d;:go;1,1!{P;$!N;D};N;bgo'</code> <code>/usr/local/xxx/config/nginx/vhost/default</code><code>.conf</code>
6、利用字元串比對取區間
6
<code>本題可以取以</code><code>"device"</code><code>開頭以 </code><code>"}"</code><code>結尾,然後将裡面的符合要求的字元串過濾計數就可以了。</code>
<code>取區間的方法:</code>
<code>sed</code> <code>-n </code><code>'/^device/,/\}$/p'</code> <code>oldboy.log</code>
<code>awk</code> <code>'/device: {/, /}/'</code> <code>oldboy.log</code>
<code>提示:</code>
<code>sed</code> <code>-n </code><code>'/區間開始辨別/,/區間結束辨別/p'</code> <code>oldboy.log</code>
7、從多少行到多少行
<code>sed</code> <code>-n </code><code>'16214,24400p'</code> <code>default.err >mysqlerr.log</code>
8、将\n換行符替換成空格(sed進階應用很重要)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<code>[root@localhost ~]</code><code># chkconfig --list | grep xyz| awk '{print $1}' | sed 's/\/n/ /g'</code>
<code>xyz_apache</code>
<code>xyz_app</code>
<code>xyz_clamd</code>
<code>xyz_ctasd_in</code>
<code>xyz_ctasd_out</code>
<code>xyz_dovecot</code>
<code>xyz_dspam</code>
<code>xyz_mysqld</code>
<code>xyz_nginx</code>
<code>xyz_openldap</code>
<code>xyz_postfix</code>
<code>xyz_postgresql</code>
<code>xyz_postgrey</code>
<code>xyz_redis</code>
<code>xyz_spamassassin</code>
<code>方法1:</code>
<code>[root@localhost ~]</code><code># chkconfig --list | grep umail| awk '{print $1}' | sed ':a;N;s/\n/ /g;ta'</code>
<code>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</code>
<code>方法2:</code>
<code>[root@localhost ~]</code><code># chkconfig --list | grep umail| awk '{print $1}' | tr "\n" " "</code>
<code>指令擴充:</code>
<code>[root@localhost~]</code><code># for i in `chkconfig --list | grep xyz| awk '{print $1}' | sed ':a;N;s/\n/ /g;ta'`; do /etc/init.d/$i status; done</code>
本文轉自 sailikung 51CTO部落格,原文連結:http://blog.51cto.com/net881004/2053962,如需轉載請自行聯系原作者