发布新日志

  • LINUX下查看CPU使用率的命令

    2009-07-27 21:57:57

    1.top

    使用权限:所有使用者

    使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b]

    说明:即时显示 process 的动态

    d : 改变显示的更新速度,或是在交谈式指令列( interactive command) s

    q : 没有任何延迟的显示速度,如果使用者是有 superuser 的权限,则 top 将会以最高的优先序执行

    c : 切换显示模式,共有两种模式,一是只显示执行档的名称,另一种是显示完整的路径与名称 S : 累积模式,会将己完成或消失的子行程 ( dead child process ) CPU time 累积起来

    s : 安全模式,将交谈式指令取消, 避免潜在的危机

    i : 不显示任何闲置 (idle) 或无用 (zombie) 的行程

    n : 更新的次数,完成后将会退出 top

    b : 批次档模式,搭配 "n" 参数一起使用,可以用来将 top 的结果输出到档案内

     

    范例:

    显示更新十次后退出 ;

    top -n 10

     

    使用者将不能利用交谈式指令来对行程下命令 :

    top -s

     

    将更新显示二次的结果输入到名称为 top.log 的档案里 :

    top -n 2 -b < top.log   

    另附一个命令简介linux traceroutewindows tracert两个命令相当,跟踪网络路由

     

    2.vmstat

    正如我们之前讨论的任何系统的性能比较都是基于基线的,并且监控CPU的性能就是以上3点,运行队列、CPU使用率和上下文切换。以下是一些对于CPU很普遍的性能要求:

    1. 对于每一个CPU来说运行队列不要超过3,例如,如果是双核CPU就不要超过6

    2. 如果CPU在满负荷运行,应该符合下列分布,

    a) User Time65%70%

    b) System Time30%35%

    c) Idle0%5%

    3. mpstat

    对于上下文切换要结合CPU使用率来看,如果CPU使用满足上述分布,大量的上下文切换也是可以接受的。

    常用的监视工具有:vmstat, top,dstatmpstat.

    # vmstat 1

    procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----

    r b swpd free buff cache si so bi bo in cs us sy id wa

    0 0 104300 16800 95328 72200 0 0 5 26 7 14 4 1 95 0

    0 0 104300 16800 95328 72200 0 0 0 24 1021 64 1 1 98 0

    0 0 104300 16800 95328 72200 0 0 0 0 1009 59 1 1 98 0

    r表示运行队列的大小,

    b表示由于IO等待而block的线程数量,

    in表示中断的数量,

    cs表示上下文切换的数量,

    us表示用户CPU时间,

    sys表示系统CPU时间,

    wa表示由于IO等待而是CPU处于idle状态的时间,

    id表示CPU处于idle状态的总时间。

    dstat可以给出每一个设备产生的中断数:

    # dstat -cip 1

    ----total-cpu-usage---- ----interrupts--- ---procs---

    usr sys idl wai hiq siq| 15 169 185 |run blk new

    6 1 91 2 0 0| 12 0 13   | 0 0 0

    1 0 99 0 0 0| 0     0   6 | 0 0 0

    0 0 100 0 0 0| 18 0   2 | 0 0 0

    0 0 100 0 0 0| 0     0   3 | 0 0 0

    我们可以看到这里有3个设备号15169185.设备名和设备号的关系我们可以参考文件/proc/interrupts, 这里185代表网卡eth1.

    # cat /proc/interrupts

    CPU0

    0: 1277238713 IO-APIC-edge timer

    6: 5 IO-APIC-edge floppy

    7: 0 IO-APIC-edge parport0

    8: 1 IO-APIC-edge rtc

    9: 1 IO-APIC-level acpi

    14: 6011913 IO-APIC-edge ide0

    15: 15761438 IO-APIC-edge ide1

    169: 26 IO-APIC-level Intel 82801BA-ICH2

    185: 16785489 IO-APIC-level eth1

    193: 0 IO-APIC-level uhci_hcd:usb1

    mpstat可以显示每个CPU的运行状况,比如系统有4CPU。我们可以看到:

    # mpstat –P ALL 1

    Linux 2.4.21-20.ELsmp (localhost.localdomain) 05/23/2006

    05:17:31 PM CPU %user %nice %system %idle intr/s

    05:17:32 PM all 0.00 0.00 3.19 96.53 13.27

    05:17:32 PM 0 0.00 0.00 0.00 100.00 0.00

    05:17:32 PM 1 1.12 0.00 12.73 86.15 13.27

    05:17:32 PM 2 0.00 0.00 0.00 100.00 0.00

    05:17:32 PM 3 0.00 0.00 0.00 100.00 0.00

    总结的说,CPU性能监控包含以下方面:

    检查系统的运行队列,确保每一个CPU的运行队列不大于3.

    确保CPU使用分布满足70/30原则(用户70%,系统30%)。

    如果系统时间过长,可能是因为频繁的调度和改变优先级。

    CPU Bound进程总是会被惩罚(降低优先级)而IO Bound进程总会被奖励(提高优先级)。

     

    4.prstat 命令

    要显示系统上当前运行的进程和项目的各种统计信息,请使用带有 -J 选项的 prstat 命令:

     

    % prstat -J

              PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP

     21634 jtd      5512K 4848K cpu0    44    0   0:00.00 0.3% prstat/1

       324 root       29M   75M sleep   59    0   0:08.27 0.2% Xsun/1

     15497 jtd        48M   41M sleep   49    0   0:08.26 0.1% adeptedit/1

       328 root     2856K 2600K sleep   58    0   0:00.00 0.0% mibiisa/11

      1979 jtd      1568K 1352K sleep   49    0   0:00.00 0.0% csh/1

      1977 jtd      7256K 5512K sleep   49    0   0:00.00 0.0% dtterm/1

       192 root     3680K 2856K sleep   58    0   0:00.36 0.0% automountd/5

      1845 jtd        24M   22M sleep   49    0   0:00.29 0.0% dtmail/11

      1009 jtd      9864K 8384K sleep   49    0   0:00.59 0.0% dtwm/8

       114 root     1640K  704K sleep   58    0   0:01.16 0.0% in.routed/1

       180 daemon   2704K 1944K sleep   58    0   0:00.00 0.0% statd/4

       145 root     2120K 1520K sleep   58    0   0:00.00 0.0% ypbind/1

       181 root     1864K 1336K sleep   51    0   0:00.00 0.0% lockd/1

       173 root     2584K 2136K sleep   58    0   0:00.00 0.0% inetd/1

       135 root     2960K 1424K sleep    0    0   0:00.00 0.0% keyserv/4

    PROJID    NPROC  SIZE   RSS MEMORY      TIME  CPU PROJECT

        10       52  400M  271M    68%   0:11.45 0.4% booksite

         0       35  113M  129M    32%   0:10.46 0.2% system

     

    Total: 87 processes, 205 lwps, load averages: 0.05, 0.02, 0.02

    要显示系统上当前运行的进程和任务的各种统计信息,请使用带有 -T 选项的 prstat 命令:

     

    % prstat -T

       PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP

     23023 root       26M   20M sleep   59    0   0:03:18 0.6% Xsun/1

     23476 jtd        51M   45M sleep   49    0   0:04:31 0.5% adeptedit/1

     23432 jtd      6928K 5064K sleep   59    0   0:00:00 0.1% dtterm/1

     28959 jtd        26M   18M sleep   49    0   0:00:18 0.0% .netscape.bin/1

     23116 jtd      9232K 8104K sleep   59    0   0:00:27 0.0% dtwm/5

     29010 jtd      5144K 4664K cpu0    59    0   0:00:00 0.0% prstat/1

       200 root     3096K 1024K sleep   59    0   0:00:00 0.0% lpsched/1

       161 root     2120K 1600K sleep   59    0   0:00:00 0.0% lockd/2

       170 root     5888K 4248K sleep   59    0   0:03:10 0.0% automountd/3

       132 root     2120K 1408K sleep   59    0   0:00:00 0.0% ypbind/1

       162 daemon   2504K 1936K sleep   59    0   0:00:00 0.0% statd/2

       146 root     2560K 2008K sleep   59    0   0:00:00 0.0% inetd/1

       122 root     2336K 1264K sleep   59    0   0:00:00 0.0% keyserv/2

       119 root     2336K 1496K sleep   59    0   0:00:02 0.0% rpcbind/1

       104 root     1664K  672K sleep   59    0   0:00:03 0.0% in.rdisc/1

    TASKID    NPROC  SIZE   RSS MEMORY      TIME  CPU PROJECT                    

       222       30  229M  161M    44%   0:05:54 0.6% group.staff                

       223        1   26M   20M   5.3%   0:03:18 0.6% group.staff                

        12        1   61M   33M   8.9%   0:00:31 0.0% group.staff                

         1       33   85M   53M    14%   0:03:33 0.0% system                     

     

    Total: 65 processes, 154 lwps, load averages: 0.04, 0.05, 0.06      


    -J -T 选项不能一起使用。

     

  • Shell编程常识(二)【转】

    2009-06-02 16:03:46

    .chmod
    chmod [who] operator [permission] filename

    who
        u    所有者
        g    组
        o    其他用户
        a    所有用户

    operator
        +    增加权限
        -    取消权限
        =    设定权限

    permission
        r    读权限
        w    写权限
        x    执行权限
        s    文件属主和组set-ID
        l    给文件加锁,使其他用户无法访问
        t    粘性位 (如果一个目录上出现粘着位, 这就意味着目录中的文件只有其属主才可以删除,即使某个同组用户具有和属主同等的权限。不过有的系统上,这个规则并不严格; 如果在文件列表时看到t, 则意味着脚本或程序在执行时会被放在交换区(虚存),不过由于当今的内存价格如此这低,大可不必理会文件的t应用)

    示例:
        chmod a+x file
        chmod og-w file
        chmod g+w file
        chmod u+x file
        chmod go+x file

       

    .suid/guid
    chmod u+s file

    # ls -l a.html
    -rw-r--r-- a.html

    # chmod u+s a.html

    # ls -l a.html
    -rwSr--r-- a.html


    .find
    find path -options [-print -exec -ok]
    -exec   find命令将匹配的文件执行exec所给出的shell命令 -exec cmd {} \; (注意: {} 和 \;间的空格)
    -ok     同-exec只不过要求对命令的执行进行确认

    -name   匹配文件名
             find . -name "*.txt" -print
             find . -name "[a-z]*" -print
             find . -name *        -print    或 find . -print
             find . -name "[a-z][a-z][0--9][0--9].txt"   -print

    -perm   匹配权限
             find . -perm 755   -print
             find . -perm -007 -print

    -prune 忽略目录 (如果使用了-depth选项,则忽略-prune选项) [没有试用成功]
             find /apps -name "/apps/bin" -prune -o -print

    -user   匹配用户
             find . -user cnscn -print   
            
    -nouser 帐户属主已被删除的文件
             find . -nouser -print

    -group
    -nogroup

    -mtime +-n 按更改时间查找文件
             find . -mtime -5 -print    查找5日内更改过的文件
             find . -mtime +5 -print    查找5日前更改过的文件

    -newer file    查找比file更改时间新的文件
    ! -newerfile2 查找比file更改时间旧的文件
             find . -newer    a.txt
             find . ! -newer a.txt

    # date
        2006年 09月 13日 星期三 11:16:07 CST
    # touch -t 0609120000 a.txt    创建文件a.txt并指定它的更改时间
    # ls -l a.txt
    -rw-r--r-- 1 root root 0 09-12 00:00 a.txt


    -type
         f   
         b
         d
         c
         p
         l


    -maxdepth    指定递归操作的最在目录深度
    # find .   -maxdepth 1 -type d
       .
       ./tmp
       ./mrtg-2.14.5
       ./piano
       ./tidy4aug00


    -mount     仅在当前的文件系统内进行操作
    -fstype


    -cpio      将查询结果进行cpio命令进行操作
         find etc home apps -depth -print | cpio -ivcdC65536 -o /dev/rmt0

    -exec cmd {} \;
    -ok   cmd {} \;
         find . -type f -exec ls -l {} \;
         find . -type f -exec rm -f {} \;

         find . -type f -ok   ls -l {} \;
         find . -type f -ok   rm -f {} \;


    -size n[c]
    -depth
    -follow

    .xargs
    功能类似 -exec , 但和-exec不同的是 它一次只是取得部分find查询到的文件结果, 而-exec是一次性获取全部文件结果,容易导致参数太多
      
    # find . -type f -name "*.mp3" -print | xargs ls -l
        -rw-r--r-- 1 root root 3985349 09-07 16:05 ./piano/025.mp3
        -rw-r--r-- 1 root root 3936256 09-07 16:06 ./piano/duni.mp3
        -rw-r--r-- 1 root root      28 09-07 16:23 ./piano/丝丝记忆.mp3
        -rw-r--r-- 1 root root 2423745 09-07 16:24 ./piano/异国情.mp3
        -rw-r--r-- 1 root root 2967512 09-07 16:20 ./piano/思乡曲.mp3
        -rw-r--r-- 1 root root 4567040 09-07 16:20 ./piano/星空.mp3

      
    .echo
        echo -n "string..."      不换行输出
        echo -e "string...\c"    同上
      
    .read variable1 variable2   从标准输入读内容到变量
       # read name
         sssssssssss
       # echo $name
         sssssssssss

       
    .文件重定向
    cmd   >   file                结果输出到文件file
    cmd   >> file                结果附加到文件file

    cmd 1>   file                结果输出到文件file
    cmd 2>   file                标准错误输出到文件file
    cmd   >   file 2>&1          结果和标准错误输出到文件file

    cmd < file1 > file2          命令以file1为输入, 然后以file2为输出

    cmd <<delimiter               从标准输入中读入, 直到遇到delimiter分界符
    cat >>file <<HERE             把HERE间的内容发送给文件file, 不要忘记>>
    >ssssssss
    >ssaaaaaaa
    >HERE

    cmd <&m                       把文件描述符m作为标准输入
    cmd >&m                       把标准输出重定向到文件描述符m中
    cmd <&_                       关闭标准输入


    .&&
    cmd1 && cmd2                  顺序执行命令cmd1和cmd2 (只有cmd1执行成功才会执行cmd2)

    .||
    cmd1 && cmd2                  选择执行cmd1和cmd2(如果cmd1执行失败,则执行cmd2)
    awk '{print $1,$2}' acc.qtr >qtr.tmp || echo "Error"     #如果前面执行错误,则报告错误

    .()      一般很少单独执行而是和&& || 组合运行                     
    (cmd1;cmd2;cmd3...)           在当前shell中执行一组命令
    mail ... || (echo "Error" | mail; exit)

    .{}      一般很少单独执行而是和&& || 组合运行
    {cmd1;cmd2;cmd3...}           在子shell中执行一组命令


    .正则表达式
    ^                             匹配行首
    $                             匹配行尾
    *                             匹配0个或多个前面的单个字符
    []                            匹配[]内的任意一个字符
    \                             转义
    pattern\{n\}                  匹配n次pattern
    pattern\{n,\}                 匹配最少n次pattern
    pattern\{n,m\}                匹配n到m次pattern

    经常使用的正则表达式
        ^
        $
        ^[the]
        [Ss]igna[lL]
        [mayMAY]
        ^User$
        [tty]$                 以tty结尾的行
        \.
        ^d..x..x..x
        ^[^l]
        [.*0]
        [000*]                  000或更多个
        [iI]                    i或I
        [iI][nN]                in或iN或In或IN
        [^$]                    空行
        ^......$                含6个字符的行
        [a-zA-Z]                任意单个字符
        [a-z][a-z]*             到少一个小写字母
        [^0-9\$]                非数字或$
        [^0-0A-Za-z]            非数字或字母
        [123]                   1或2或3
        [Dd]evice               device或Device
        De..ce                  以De开始中间两个任意字符最后是ce
        \^q                     以^q开始的行
        ^.$                     只有一个字符的行
        ^\.[0-9][0-9]           一个句点和两个数字开始的行
        '"Device"'              Device
        De[Vv]ice\.             Device 或 device
        [0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\}                    dd-mm-yyyy
        [0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}      匹配IP地址
        [^.*$]                  匹配任意行


    .grep
        -E                      匹配正则表达式

        grep -ERv "a|g" ./      查询不含字符a与g的文件

    .awk
    域分隔符
         awk -F":" '{print $1}' a.txt
         awk -F:    '{print $1}' a.txt

    awk脚本  
         awk -f a.wak a.txt

    BEGIN/END
         awk 'BEGIN {print "Name     Belt\n-------------------"} {print $1"\t"$4} END{print "END..." } ' a.txt
        
    $n ~ /正则/    
    awk '$0 ~ /Brown'               a.txt
    awk '$0 !~ /Brown/'             a.txt


    条件
    awk '$3=="48" {print $0}'       a.txt
    awk '{if($3~/48/) print $0}'    a.txt
    awk '{if($3!~/48/) print $0}'   a.txt

    awk '$4 != "Tom" {print $0}'    a.txt

    awk '{if($6 <= $7) print $0 }' a.txt

    awk '/[Gg]reen/'                a.txt
    awk '$1~/^ ... a/'              a.txt
    awk '$0 ~ /(Ye|Br)/'            a.txt

    awk '/^48/'                     a.txt
    awk '{if($1=="Y" && $2=="G") print $0}' a.txt
    awk '{if($1=="Y" || $2=="G") print $0}' a.txt


    内置参数
    ARGC                           参数个数
    ARGV                           参数数组
    ENVIRON                        支持队列中系统环境变量的使用
    FILENAME                       awk浏览的文件名
    FNR                            浏览文件的记录数
    NR                             已读出的记录数
    FS                             设置输入域分隔符,等 价于命令行-F选项
    NF                             记录的域的个数
    OFS                            输出域分隔符
    RS                             控制记录分隔符

    # awk -F"S" '{print ENVIRON["HOME"] }' a.txt
    /root

    awk '{name=$1; belts=$4; if(belts ~ /Y/) print name" is belt" }' a.txt

    awk '(total+=$6); END{ print "Total:" total}'         打印每一行及END一行

    awk '{(total+=$6)}; END{ print "Total:" total}'       只打印END一行

    ls -l | awk '/^[^d]/ {print $9"\t"$5} {total+=$5} END{print "Total: KB:" total}'


    gsub(r,s)                    在$0中用s替代r
    #awk -F"S" 'gsub(/a/,"xxxxxxxxx"){print $0 }' a.txt
    Return-Pxxxxxxxxxth: <cnstest@sinxxxxxxxxx.com>
    by mx28.mxxxxxxxxxil.sohu.com (Postfix) with ESMTP id 747AD00442AD

    gsub(r,s,str)                在str中用s替代r
    # awk -F":" 'gsub(/a/,"xxxxxxxxx",$1){print $0 }' a.txt
       Return-Pxxxxxxxxxth <cnstest@sina.com>
       by mx28.mxxxxxxxxxil.sohu.com (Postfix) with ESMTP id 747AD00442AD
       for <cnstest%sohu.com@mx28.mxxxxxxxxxil.sohu.com>; Mon, 11 Sep 2006 18 03 11 +0800 (CST)


    index(s,t)                   返回字符串s中t的第一次出现的位置
    length(s)                    返回串s的长度
    match(s,r)                   测试s是否包含匹配r的字符串
    # awk 'BEGIN{print match("ANCD",/d/)}'
    0
    # awk 'BEGIN{print match("ANCD",/D/)}'
    4

    split(s,arr,fs)                在fs上把分成数组arr, 返回数组长度
    # awk 'BEGIN{print split("12#33#44#555#66",tmparr,"#")": "; print tmparr[0]" "tmparr[1]" "tmparr[3]}'
       5: 12 44

    sprint(fmt,exp)              返回经fmt格式化过的exp
    sub(r,s)                     用$0中最左边最长的子串代替s
    # awk -F":" 'sub(/a/,"xxxxxxxxx"){print $0 }' a.txt
       Return-Pxxxxxxxxxth: <cnstest@sina.com>
       by mx28.mxxxxxxxxxil.sohu.com (Postfix) with ESMTP id 747AD00442AD
       for <cnstest%sohu.com@mx28.mxxxxxxxxxil.sohu.com>; Mon, 11 Sep 2006 18:03:11 +0800 (CST)

    substr(s,p)                  返回s中从p开始的后续部分
    substr(s,p,n)                返回字符串s中从p开始长度为n的后续部分



    printf “%s\n”,"sssssssss"
            -                    左对齐
            width                宽度
            .prec                小数点右边的位数

            %f                   浮点数
            #awk 'BEGIN{printf "%f\n",999}'
            999.000000

            %c                   字符
            %d                   整数
            %e                   科学记数法
            %g                   awk决定使用哪种浮点数
            %o                   八进制
            %s                   字符串
            %x                   十六进制


    向awk传递参数
    # awk '{ print AGE}' AGE=777 a.txt
        777
        777

    FS的用法
    #cat a.awk
    !/bin/awk -f
    BEGIN{
        FS=":"
    }

    {
       print $1
    }

    数组
    # awk 'BEGIN{arr["a"]="b"; arr["b"]="c"; for(key in arr) print arr[key] }'
       b
       c

    =======================================

    .sed 是一个非交互性文本流编辑器
    .sed [选项] sed命令     File
    sed [选项] -f sed脚本 File
    选项:
         -n       不输出结果
         -c
         -f

    命令:
         p       打印匹配行
         =       显示行号
         s       替换
         r       从另一个文件中读取文本
         w       写文本到一个文本
         q       第一个模式匹配完成后退出或立即退出
         l       显示与八进制ASCII代码等价的控制字符
         {}      在定位行执行的命令组
         g       将模式2粘贴到/pattern n/
         y       传送字符
         n       延续到下一输入行,允许跨行的模式匹配语句

    数据行定位
    x                    x为一行号,如1
    x,y                  从x,到y的行
    /pattern/            包含模式的行/abc/或/[a-z]
    /pattern/pattern/    同时包含两个模式的行/disk/disks/
    pattern/,x           在给定行号上查询含模式的行/disk/,3
    x,/pattern/          通过行号和模式查询匹配行 3,/disk/
    x,y!                 查询不含指定行号x和y的行, 如 1,2!


    #head a.txt
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@sohu.com

    .在每一行后加入一行
    #sed 'absssssssssssssss' a.txt        (此处a为添加一行的命令, bssssss... 为要添加的内容)
    Return-Path: <cnstest@sina.com>
    bsssssssssssssss
    Delivered-To: cnstest@sohu.com
    bsssssssssssssss

    #sed -n '2p' a.txt                    (显示第二行)
    Delivered-To: cnstest@sohu.com

    #sed -n '2,4p' a.txt                  (显示第二行到第四行)
    Delivered-To: cnstest@sohu.com
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])
    by mx28.mail.sohu.com (Postfix) with ESMTP id 747AD00442AD

    #sed -n '/sohu/p' a.txt               (显示含sohu的行)
    Delivered-To: cnstest@sohu.com
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])

    #sed -n '1,$p' a.txt                  (显示整个文件)
    #sed -n 'p' a.txt

    #sed -n '/.*ohu/p' a.txt              (任意字符)
    Delivered-To: cnstest@sohu.com
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])
    by mx28.mail.sohu.com (Postfix) with ESMTP id 747AD00442AD

    #sed -n '$p' a.txt                    (显示第2行)
    Delivered-To: cnstest@sohu.com

    #sed -ne '/sohu/p' -e '/sohu/=' a.txt (显示文本及行号)
       Delivered-To: cnstest@sohu.com
       2
       Received: from sohumx180.sohu.com (unknown [192.168.132.180])
       3
            by mx28.mail.sohu.com (Postfix) with ESMTP id 747AD00442AD
       4

    .$cat a.sed                            (sed脚本)
    #!/bin/sed -f
    /sohu/ a\                             在含sohu的行后添加一行,内容为hello....
            hello.....

    执行脚本
    #./a.sed a.txt
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@sohu.com
            hello.....
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])
            hello.....

    .$cat a.sed                            (sed脚本)
    #!/bin/sed -f
    /sohu/ i\                             在含sohu的行前添加一行,内容为hello....
            hello.....

    #./a.sed a.txt
    Return-Path: <cnstest@sina.com>
            hello.....
    Delivered-To: cnstest@sohu.com
            hello.....
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])


    .$cat a.sed                            (sed脚本)
    #!/bin/sed -f
    /sohu/ c\                             替换含sohu的行为行hello....
            hello.....

    # ./a.sed a.txt
    Return-Path: <cnstest@sina.com>
            hello.....
            hello.....
            hello.....
            hello.....

    .$cat a.sed                            (sed脚本)
    #!/bin/sed -f
    3 c\                                  把第3行替换为hello....
            hello.....

    #./a.sed a.txt
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@sohu.com
            hello.....

    #sed '1d' a.txt                       删除第一行
    Delivered-To: cnstest@sohu.com
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])


    #sed '1,2d' a.txt                     删除第一到第2行
    Received: from sohumx180.sohu.com (unknown [192.168.132.180])
            by mx28.mail.sohu.com (Postfix) with ESMTP id 747AD00442AD


    #sed 's/sohu/xxxxxxxx/g' a.txt        把sohu替换为xxxxxxxxxx
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@xxxxxxxx.com


    #sed 's/sohu/xxxxxxxx/gw b.txt' a.txt        把sohu替换为xxxxxxxxxx, 并把替换的内容输出写到b.txt
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@xxxxxxxx.com

    #cat b.txt
    Delivered-To: cnstest@xxxxxxxx.com
    Received: from xxxxxxxxmx180.sohu.com (unknown [192.168.132.180])
    >-------by mx28.mail.xxxxxxxx.com (Postfix) with ESMTP id 747AD00442AD
    >-------for <cnstest%xxxxxxxx.com@mx28.mail.sohu.com>; Mon, 11 Sep 2006 18:03:11 +0800 (CST)
    >-------by xxxxxxxxmx180.sohu.com (Postfix) with SMTP id 7F69E05B70E1
    >-------for <cnstest@xxxxxxxx.com>; Mon, 11 Sep 2006 18:03:10 +0800 (CST)
    To: cnstest@xxxxxxxx.com


    $sed '/sohu/r b.txt' a.txt            把b.txt的内容,附加到含有sohu的行的后面

    #sed '/sohu/q' a.txt                  第一次匹配后退出
    Return-Path: <cnstest@sina.com>
    Delivered-To: cnstest@sohu.com
Open Toolbar