• sed and awk学习笔记


    sed and awk

    背景

          awk起源追溯至sed和grep,进而追溯至共同的行编辑器ed。实用工具grep来源于ed命令:g/re/p 。实用工具awk和sed有一个共同的选项-f用于指定脚本的名字:$sed -f scriptfile inputfile ,用大致相同的方法调用sed和awk:command [options] script filename   


    1、使用行编辑器ed

    1)用ed打开文件是,它显示了文件这个的字符个数并定位在最后一行,没有提示符:

    $ ed tes
    339   

    2)ed不理解的命令打印问号:
    $abc
    ?   

    3)一个命令只影响当前行,显示当前行:
    $p
    当前行的内容   

    4)跳转到第1行,然后删除那一行:
    $1
    第一行的内容
    $d
    或者:
    $1d   

    5)可以使用正则表达式,删除第一个包含单词regular的那一行:
    $/regular/d   

    6)删除包含这个正则表达式的所有行:
    $g/regular/d  

    7)用complex替代当前行上第一次出现的regular,如果没有找到会出现错误:
    $s/regular/complex/  

    8)用complex替代当前行上所有出现的regular:
    $s/regular/complex/g   

    9)匹配第一个包含单词regular的那一行,然后用用complex替代那一行上第一次出现的regular
    $/regular/s/regular/complex/g   

    10)匹配所有包含单词regular的行,然后用用complex替代这些行上出现的regular
    $/regular/s/regular/complex/g
    第一个regular是地址,第二个regular是模式,地址和模式可以不等,如果地址和模式相等也可以使用:
    $/regular/s//complex/g   


    2、sed
    调用方法:   
    1)sed [选项] sed命令  输入文件   

    2)sed [选项] -f sed脚本  输入文件   

    3)sed 脚本文件  [选项] 输入文件   

    选项:
    -e       编辑随后的指令
    -f        跟随脚本中的文件名
    -n       阻止输入行的自动输出   

    两种方式定位文本:1)  使用行号,可以是一个简单数字,或是一个行号范围。  2)  使用正则表达式进行匹配。   

    命令行上指定多重指令方式:
    1)用分号分隔指令:
    $sed 's/MA/, Massachusetts/;s/ PA/, Pennsylvania/' list   

    2)在每个指令前放置-e:
    $sed -e 's/ MA/, Massachusetts' -e 's/ PA/, Pannsylvania/' list   

    3)使用shell(不能是C shell)的分行指令功能,在输入单引号后return键,就会出项多行输入的提示符(>):
    $ sed '
    >s/ MA/, Massachusetts/
    >s/ PA/, Pannsylvania/
    >s/ CA/, California/' list   

    使用sed在文件中定位文本的方式:
    xx            为一行号,如 1
    x,y           表示行号范围从x到y,如2,5表示从第2行到第5行
    /pattern/    查询包含模式的行。如/disk/或/[a-z]/
    /pattern/pattern/         查询包含两个模式的行。如/disk/disks/ 
    pattern/,x         在给定行号上查询包含模式的行。如/ribbon/,3 
    x,/pattern/        通过行号和模式查询匹配行。如3./vdu/
    x,y!          查询不包含指定行号 x和y的行。如1,2!   

    sed编辑命令:   
    p      打印匹配行
    =     显示文件行号
    a    在定位行号后附加新文本信息
    i    在定位行号后插入新文本信息 
    d     删除定位行
    c    用新文本替换定位文本
    s     使用替换模式替换相应模式
    r     从另一个文件中读文本
    w    写文本到一个文件
    q     第一个模式匹配完成后推出或立即推出
    l      显示与八进制ASCII代码等价的控制字符
    {}    在定位行执行的命令组
    n     从另一个文件中读文本下一行,并附加在下一行
    g     将模式2粘贴到/pattern n/
    y     传送字符
    n     延续到下一输入行;允许跨行的模式匹配语句   

    sed 替换选项:

    g         缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。
    p         缺省 sed将所有被替换行写入标准输出,加p选项将使 -n选项无效。-n选项不打印输出 结果。
    w  文件名      使用此选项将输出定向到一个文件。 

    显示文件中的控制字符方式:
    1)使用 cat -v filename命令,屏幕会乱叫且到处都是一些垃圾字符,这可以确知文件中包含有控制字符(一些系统中使用cat  filename而不是cat-v来查看非打印字符)   

    2)使用 sed 显示文件中的控制字符

    向文件中插入控制字符的方法:
         各系统控制字符键值可能不同,主要取决于其映射方式(例如使用terminfo或termcap)。 如果要在文本文件中插入控制字符F1键,使用vi查看其键值,操作如下: 1)启动vi。    2)进入插入模式。 3)按下<Ctrl>键,然后按 <v>键  (出现 a^)。 4)释放上述两个键。5)按下F1键(显示 [OP])。  6)按下<ESC>键(显示 F1键值)   

    快速一行命令
    下面是一些一行命令集。([]表示空格, [ ] 表示tab键)
    ‘s/.$//g’    删除以句点结尾行
    ‘-e /abcd/d’    删除包含 abcd的行
    ‘s/[][][]*/[]/g ’    删除一个以上空格,用一个空格代替
    ‘s/^[][]*//g’     删除行首空格
    ‘s/.[][]*/[]/g’      删除句点后跟两个或更多空格,代之以一个空格
    ‘/^$/d’      删除空行
    ‘s/^.//g’     删除第一个字符
    ‘s/COL(...)//g’     删除紧跟 COL的后三个字母
    ‘s/^///g’     从路径中删除第一个
    ‘s/[]/[]//g’     删除所有空格并用 tab键替代
    ‘S/^[]//g’     删除行首所有 tab键
    ‘s/[]*//g’     删除所有 tab键   

    命令行例子:
    [root@redhat script]#cat quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed  '2p' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '2p' quote.txt 
    It was an evening of splendid music and company.
    [root@redhat script]#
    [root@redhat script]#sed -n '2'p quote.txt 
    It was an evening of splendid music and company.
    [root@redhat script]#
    [root@redhat script]#sed -n '1,3p' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    [root@redhat script]#
    [root@redhat script]#sed -n '/Neave/'p quote.txt 
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '/The/'p quote.txt 
    The honeysuckle band played all night long for only $90.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '/$/'p quote.txt 
    The honeysuckle band played all night long for only $90.
    [root@redhat script]#
    [root@redhat script]#sed -n '1,$'p quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '3,/The/'p quote.txt 
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '/.*ing/'p quote.txt 
    It was an evening of splendid music and company.
    [root@redhat script]#
    [root@redhat script]#sed -n '1'p quote.txt 
    The honeysuckle band played all night long for only $90.
    [root@redhat script]#
    [root@redhat script]#sed -n '$'p quote.txt 
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed  '/music/=' quote.txt 
    The honeysuckle band played all night long for only $90.
    2
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n '/music/=' quote.txt 
    2
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed  '/company/ aaaaaaaaaaaaaaaaaaaaaaaa' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    aaaaaaaaaaaaaaaaaaaaaa
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#cat append.sed
    #!/bin/sed -f
    /company/ a
    1111111111111111111111111
    2222222222222222222222222
    3333333333333333333333333
    [root@redhat script]#
    [root@redhat script]#./append.sed quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    1111111111111111111111111
    2222222222222222222222222
    3333333333333333333333333
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed '4 i5555555555555' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    5555555555555
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed '4 c5555555555555' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    5555555555555
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed '/Too/ c5555555555555' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    5555555555555
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed 'c5555555555555' quote.txt 
    5555555555555
    5555555555555
    5555555555555
    5555555555555
    [root@redhat script]#
    [root@redhat script]#sed '1d' quote.txt 
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed '1,3d' quote.txt 
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed '$d' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed '/Neave/d' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed 's/night/NIGHT/' quote.txt 
    The honeysuckle band played all NIGHT long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/night/NIGHT/' quote.txt 
    [root@redhat script]#
    [root@redhat script]#sed -n 's/night/NIGHT/'p quote.txt
    The honeysuckle band played all NIGHT long for only $90.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/$//'p quote.txt
    The honeysuckle band played all night long for only 90.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/The/Wow!/g'p quote.txt
    Wow! honeysuckle band played all night long for only $90.
    Wow! local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed  's/The/Wow!/g' quote.txt
    Wow! honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    Wow! local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#cat quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/"Hello" &/p' quote.txt 
    The local "Hello" nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/"Hello" &/'p quote.txt 
    The local "Hello" nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/aaaaaaaaa &/'p quote.txt 
    The local aaaaaaaaa nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/aaaaaaaaa&/'p quote.txt 
    The local aaaaaaaaanurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/11111111 &/p' quote.txt
    The local 11111111 nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed -n 's/nurse/& 11111111/p' quote.txt
    The local nurse 11111111 Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#ll
    总用量 68
    -rw-r--r--  1 root root    1 2011/12/06 05:12:25 1
    -rwxrwxrwx  1 root root  103 2011/09/22 02:10:07 aa.bak
    -rwxr-xr-x  1 root root  121 2011/12/06 06:51:07 aa.sh
    -rwxr--r--  1 root root  107 2011/12/06 16:27:45 append.sed
    -rwxr--r--  1 root root  154 2011/12/06 15:11:47 arraytest.awk
    drwxr-xr-x  2 root root 4096 2011/11/26 23:27:11 CD
    drwxr-xr-x  2 root root 4096 2011/12/06 13:32:53 Demo
    -rwxr-xr-x  1 root root  123 2011/12/06 14:49:16 fieldcheck.awk
    -rwxr-xr-x  1 root root   44 2011/09/22 12:11:21 first.sh
    -rwxrwxrwx  1 root root  195 2011/12/06 06:07:48 grade.txt
    -rwxrwxrwx  1 root root  303 2011/11/27 03:04:04 list
    -rwxrwxrwx  1 root root  101 2011/11/27 03:52:13 nameState
    -rwxr--r--  1 root root   91 2011/12/06 14:45:51 passwd.awk
    -rwxr-xr-x  1 root root  202 2011/12/06 15:54:36 quote.txt
    -rwxrwxrwx  1 root root   44 2011/11/27 03:24:34 sedscr
    -rwxr--r--  1 root root  237 2011/12/06 14:24:56 student_tot.awk
    -rwxrwxrwx  1 root root   90 2011/09/21 20:39:22 test.sh
    [root@redhat script]#
    [root@redhat script]#sed '1,2 w sedout1' quote.txt
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#ll sedout1 
    -rw-r--r--  1 root root 106 2011/12/06 17:01:31 sedout1
    [root@redhat script]#
    [root@redhat script]#cat sedout1 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    [root@redhat script]#
    [root@redhat script]#vi sedex.txt
    [root@redhat script]#
    [root@redhat script]#cat sedex.txt 
    fffffffffffffffffff
    ddddddddddddddddddd
    [root@redhat script]#
    [root@redhat script]#sed '/company./r sedex.txt' quote.txt 
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    fffffffffffffffffff
    ddddddddddddddddddd
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed '/.a.*/'p quote.txt 
    The honeysuckle band played all night long for only $90.
    The honeysuckle band played all night long for only $90.
    It was an evening of splendid music and company.
    It was an evening of splendid music and company.
    Too bad the disco floor fell throught at 23:10.
    Too bad the disco floor fell throught at 23:10.
    The local nurse Miss P.Neave was in attendance.
    The local nurse Miss P.Neave was in attendance.
    [root@redhat script]#
    [root@redhat script]#sed '/.a.*/'q quote.txt 
    The honeysuckle band played all night long for only $90.
    [root@redhat script]#
    [root@redhat script]#sed '/.a.*/q' quote.txt 
    The honeysuckle band played all night long for only $90.
    [root@redhat script]#
    [root@redhat script]# 
    [root@redhat script]#vi func.txt
    [root@redhat script]#
    [root@redhat script]#cat func.txt 
    This is the F1 key:P
    This is the F2 key:Q
    [root@redhat script]#
    [root@redhat script]#cat -v func.txt 
    This is the F1 key:^[OP
    This is the F2 key:^[OQ
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed -n '1,$'l func.txt
    This is the F1 key:33OP$
    This is the F2 key:33OQ$
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#sed -n '1,$l' func.txt
    This is the F1 key:33OP$
    This is the F2 key:33OQ$
    [root@redhat script]#
    [root@redhat script]#sed -n 's/^[OP/f1/g' func.txt
    [root@redhat script]#sed -n 's/^[OP/f1/g'p func.txt
    This is the F1 key:f1
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#NAME="It's a go situation"
    [root@redhat script]#REPLACE="GO"
    [root@redhat script]#
    [root@redhat script]#echo $NAME | sed 's/go/aaaaaa/g'
    It's a aaaaaa situation
    [root@redhat script]#
    [root@redhat script]#echo $NAME | sed 's/go/GO/g'
    It's a GO situation
    [root@redhat script]#
    [root@redhat script]#echo $NAME | sed 's/go/$REPLACE/g'
    It's a $REPLACE situation
    [root@redhat script]#
    [root@redhat script]#echo $NAME | sed "s/go/$REPLACE/g"
    It's a GO situation
    [root@redhat script]#
    [root@redhat script]#echo $PWD
    /root/yzh/script
    [root@redhat script]#
    [root@redhat script]#echo $PWD | sed 's/^///g'
    root/yzh/script
    [root@redhat script]#
    [root@redhat script]#echo "Mr Willis" | sed 's/Mr /& Bruce/g'
    Mr  BruceWillis
    [root@redhat script]#
    [root@redhat script]#echo "accounts.doc" | sed 's/^.//g'
    ccounts.doc
    [root@redhat script]#
    [root@redhat script]#echo "accounts.doc" | sed 's/.doc//g'
    accounts
    [root@redhat script]#
    [root@redhat script]#echo "accounts" | sed 's/$/.doc/g'
    accounts.doc
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#cat list 
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Eric Adams, 20 Post Road, Sudbury MA
    Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston MA
    [root@redhat script]#
    [root@redhat script]#sed 's/MA/Massachusetts/' list
    John Daggett, 341 King Road, Plymouth Massachusetts
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Eric Adams, 20 Post Road, Sudbury Massachusetts
    Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston Massachusetts
    [root@redhat script]#cat sedscr
    s/ MA/, Massachusetts/
    s/ PA/,Pennsylvania/
    [root@redhat script]#
    [root@redhat script]#sed -f sedscr list
    John Daggett, 341 King Road, Plymouth, Massachusetts
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls,Pennsylvania
    Eric Adams, 20 Post Road, Sudbury, Massachusetts
    Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston, Massachusetts
    [root@redhat script]#
    [root@redhat script]#sed -n -e 's/MA/Massachusetts/p' list
    John Daggett, 341 King Road, Plymouth Massachusetts
    Eric Adams, 20 Post Road, Sudbury Massachusetts
    Sal Carpenter, 73 6th Street, Boston Massachusetts
    [root@redhat script]#
    [root@redhat script]#awk '{print $1}' list
    John
    Alice
    Orville
    Terry
    Eric
    Hubert
    Sal
    [root@redhat script]#
    [root@redhat script]#awk '{print $0}' list
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Eric Adams, 20 Post Road, Sudbury MA
    Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston MA
    [root@redhat script]#
    [root@redhat script]#cat list
    John Daggett, 341 King Road, Plymouth MA
    Alice Ford, 22 East Broadway, Richmond VA
    Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Eric Adams, 20 Post Road, Sudbury MA
    Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Sal Carpenter, 73 6th Street, Boston MA
    [root@redhat script]#
    [root@redhat script]#awk '/MA/' list
    John Daggett, 341 King Road, Plymouth MA
    Eric Adams, 20 Post Road, Sudbury MA
    Sal Carpenter, 73 6th Street, Boston MA
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '/MA/ {print $1}' list
    John
    Eric
    Sal
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk -F, '/MA/ {print $1}' list
    John Daggett
    Eric Adams
    Sal Carpenter
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk -F, '{print $1;print $2;print $3}' list
    John Daggett
    341 King Road
    Plymouth MA
    Alice Ford
    22 East Broadway
    Richmond VA
    Orville Thomes
    11345 OaK Bridge Road
    Tulsa OK
    Terry Kalkas
    402 Lans Road
    Beaver Falls PA
    Eric Adams
    20 Post Road
    Sudbury MA
    Hubert Sims
    328A Bayshore Pkwy
    Mountain View CA
    Sal Carpenter
    73 6th Street
    Boston MA
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk -F, '{print $2}' list
    341 King Road
    22 East Broadway
    11345 OaK Bridge Road
    402 Lans Road
    20 Post Road
    328A Bayshore Pkwy
    73 6th Street
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#cat nameState 
    s/ CA/, California/
    s/ MA/, Massachusetts/
    s/ OK/, Oklahoma/
    s/ PA/, Pennsylvania/
    s/ VA/, Virginia/
    [root@redhat script]#sed -f nameState list | awk -F, '{print $4}'
    Massachusetts
    Virginia
    Oklahoma
    Pennsylvania
    Massachusetts
    California
    Massachusetts
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk  -F, '{print $3}' list
    Plymouth MA
    Richmond VA
    Tulsa OK
    Beaver Falls PA
    Sudbury MA
    Mountain View CA
    Boston MA
    [root@redhat script]#
    [root@redhat script]#awk  -F, '{print $*}' list
    awk: cmd. line:1: {print $*}
    awk: cmd. line:1:         ^ syntax error
    [root@redhat script]#awk  -F, '{print $3 "---->" $0 }' list
    Plymouth MA---->John Daggett, 341 King Road, Plymouth MA
    Richmond VA---->Alice Ford, 22 East Broadway, Richmond VA
    Tulsa OK---->Orville Thomes, 11345 OaK Bridge Road, Tulsa OK
    Beaver Falls PA---->Terry Kalkas, 402 Lans Road, Beaver Falls PA
    Sudbury MA---->Eric Adams, 20 Post Road, Sudbury MA
    Mountain View CA---->Hubert Sims, 328A Bayshore Pkwy, Mountain View CA
    Boston MA---->Sal Carpenter, 73 6th Street, Boston MA   


    3、awk

    调用awk的方式:
    1)命令行:awk [-F field-separator] 'commands' files       commands是真正的awk命令
    2)将所有awk命令保存在一个文件中,然后用wak命令解释器作为脚本的首行,并使脚本文件可以执行
    3)将awk命令保存到一个文件中,然后调用:awk -f awk-scrip input-files(s)   

    多重指令指定方式:类似sed使用分号或shell的分行指令功能   

    理解awk:将每一行看成是一条记录,每个单词看成是一个字段,默认使用空格和制表作为字段分隔符   

    awk的命令行选项:
    -F     改变字段分隔符
    -f     跟随脚本中的文件名
    -v    跟随var=value   

    awk条件操作符:  
    <         小于
    >=       大于等于
    <=       小于等于 
    ~         匹配正则表达式
    ==       等于
    !~        不匹配正则表达式
    !=        不等于
    =  +=  *=  /  =  %=  ^  =        赋值操作符
    ?         条件表达操作符
    ||  &&  !      并、与、非
    ~!~         匹配操作符,包括匹配和不匹配
    <  <=  ==  !=  >>           关系操作符
    +  -  *  /  %  ^         算术操作符
    +  +  --           前缀和后缀   

    awk内置变量:   
    ARGC              命令行参数个数
    ARGV              命令行参数排列
    ENVIRON        支持队列中系统环境变量的使用
    FILENAME       awk浏览的文件名
    FNR                浏览文件的记录数
    FS                  设置输入域分隔符,等价于命令行 -F选项
    NF                   浏览记录的域个数
    NR                  已读的记录数 
    OFS                输出域分隔符 
    ORS                输出记录分隔符 
    RS                 控制记录分隔符    
    说明:1)ARGC支持命令行中传入awk脚本的参数个数。 ARGV是ARGC的参数排列数组,其中每一元素表示为ARGV[n],n为期望访问的命令行参数。
    2)ENVIRON支持系统设置的环境变量,要访问单独变量,使用实际变量名,例如:ENVIRON[“EDITOR”]=“Vi”。
    3)FILENAME支持awk脚本实际操作的输入文件。因为 awk可以同时处理许多文件,因此如果访问了这个变量,将告之系统目前正在浏览的实际文件。
    4)FNR支持awk目前操作的记录数。其变量值小于等于  NR。如果脚本正在访问许多文件, 每一新输入文件都将重新设置此变量。
    5)FS用来在awk中设置域分隔符,与命令行中 -F选项功能相同。缺省情况下为空格。如果用 逗号来作域分隔符,设置 FS=","。
    6)NF支持记录域个数,在记录被读之后再设置。OFS允许指定输出域分隔符,缺省空格。如果想设置为#,写入OFS="#"。 ORS为输出记录分隔符,缺省为新行( )。
    7)RS是记录分隔符,缺省为新行 ( )。   

    awk内置字符串函数:   
    gsub(r,s)           在整个 $0中用s替代r 
    gsub(r,s,t)         在整个 t中用s替代r 
    index(s,t)          返回s中字符串 t的第一位置 
    length(s)          返回s长度
    match(s,r)          测试s是否包含匹配 r的字符串 
    split(s,a,fs)          在fs上将s 分成序列 a 
    sprint(fmt,exp)     返回经 fmt格式化后的 
    exp sub(r,s)          用$0中最左边最长的子串代替 s
    substr(s,p)            返回字符串 s中从p开始的后缀部分
    substr(s,p,n)         返回字符串 s 中从p开始长度为 n的后缀部分

    awk中屏蔽序列:
               退格键 
                tab键
    f            走纸换页 
    ddd        八进制值
               新行
    c            任意其他特殊字符,例如 \为反斜线符号
                回车键

    awk printf修饰符:
    -             左对齐
    Width           域的步长,用0表示0步长
    .prec           最大字符串长度,或小数点右边的位数   

    awk printf格式:
    %c           ASCII字符
    %d           整数
    %e           浮点数,科学记数法
    %f           浮点数,例如( 123.44)
    %g           awk决定使用哪种浮点数转换 e或者f
    %o           八进制数
    %s           字符串
    %x           十六进制数   

    命令行实例:
    [root@redhat script]#cat grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '{print $1$2}' grade.txt 
    M.Tansley05/99
    J.Lulu06/99
    P.Bunny02/99
    J.Troll07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#awk '{print $1 $2}' grade.txt 
    M.Tansley05/99
    J.Lulu06/99
    P.Bunny02/99
    J.Troll07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '{print $1,$2}' grade.txt 
    M.Tansley 05/99
    J.Lulu 06/99
    P.Bunny 02/99
    J.Troll 07/99
    L.Tansley 05/99
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '{print $1" "$2}' grade.txt 
    M.Tansley05/99
    J.Lulu06/99
    P.Bunny02/99
    J.Troll07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#awk '{printf "%-8s %s ",$1,$2}' grade.txt 
    M.Tansley05/99
    J.Lulu  06/99
    P.Bunny 02/99
    J.Troll 07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "Name Belt ----------------------"}  {printf "%-8s %s ",$1,$2}' grade.txt 
    Name Belt
    ----------------------
    M.Tansley05/99
    J.Lulu  06/99
    P.Bunny 02/99
    J.Troll 07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "Name Belt ----------------------"}  {printf "%-8s %s ",$1,$2}' grade.txt 
    NameBelt
    ----------------------
    M.Tansley05/99
    J.Lulu  06/99
    P.Bunny 02/99
    J.Troll 07/99
    L.Tansley05/99
    [root@redhat script]#
    [root@redhat script]#awk '{print "$1 $2"}' grade.txt 
    $1$2
    $1$2
    $1$2
    $1$2
    $1$2
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "Name Belt ----------------------"}  {printf "%-8s %s ",$1,$2} END {print "end-of-report"}' grade.txt 
    NameBelt
    ----------------------
    M.Tansley05/99
    J.Lulu  06/99
    P.Bunny 02/99
    J.Troll 07/99
    L.Tansley05/99
    end-of-report
    [root@redhat script]#
    [root@redhat script]#cat grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '$1 ~ /Bunny/' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#
    [root@redhat script]#awk '$0 ~ /Bunny/' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#awk '$2 ~ /Bunny/' grade.txt 
    [root@redhat script]#
    [root@redhat script]#awk '$0 ~ /Brown/' grade.txt 
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '{$0 ~ /Brown/}' grade.txt 
    [root@redhat script]#
    [root@redhat script]#awk '{if($4 ~ /Brown/) print $0}' grade.txt 
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '{$0~/Brown/}' grade.txt 
    [root@redhat script]#
    [root@redhat script]#awk '{if($3~/48/) print $0}' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#awk '$3=="48" {print $0}' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#
    [root@redhat script]#awk '$0 !~ /Brown/' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#
    [root@redhat script]#awk '{$4 !~ /Brown/' grade.txt 
    awk: cmd. line:2: (END OF FILE)
    awk: cmd. line:2: syntax error
    [root@redhat script]#awk '{if($4!~/Brown/) print $0}' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#
    [root@redhat script]#awk '$4 != "Brown-2" {print $0}' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 < $7) print $1 }' grade.txt 
    M.Tansley
    J.Lulu
    [root@redhat script]#awk '{if($6 <= $7) print $1 }' grade.txt 
    M.Tansley
    J.Lulu
    J.Troll
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 < $7) print $0 "$1 Try better at the next comp" }' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44$1 Try better at the next comp
    J.Lulu    06/99 48317 green   9  24 26$1 Try better at the next comp
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 < $7) print $1 "Try better at the next comp" }' grade.txt 
    M.TansleyTry better at the next comp
    J.LuluTry better at the next comp
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 > $7) print $1}' grade.txt 
    P.Bunny
    L.Tansley
    [root@redhat script]#
    [root@redhat script]#awk '/[Gg]reen/' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    [root@redhat script]#
    [root@redhat script]#awk '$1 ~ /^...a/' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '$0 ~ /(Yellow|Brown)/' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '/^P./' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#
    [root@redhat script]#awk '{if($1 == "P.Bunny" && $4 == "Yellow") print $0}' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    [root@redhat script]#awk '{if($4 ~ /Brown/ && $4 == "Yellow") print $0}' grade.txt 
    [root@redhat script]#
    [root@redhat script]#awk '{if($4 ~ /Brown/ || $4 == "Yellow") print $0}' grade.txt 
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#
    You have new mail in /var/spool/mail/root
    [root@redhat script]#
    [root@redhat script]#cat grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk '{print NF,NR,$0} END{print FILENAME} }' grade.txt 
    awk: cmd. line:1: {print NF,NR,$0} END{print FILENAME} }
    awk: cmd. line:1:                                      ^ syntax error
    [root@redhat script]#
    [root@redhat script]#awk '{print NF,NR,$0} END {print FILENAME} }' grade.txt 
    awk: cmd. line:1: {print NF,NR,$0} END {print FILENAME} }
    awk: cmd. line:1:                                       ^ syntax error
    [root@redhat script]#awk '{print NF,NR,$0} END{print FILENAME}' grade.txt 
    7 1 M.Tansley 05/99 48311 Green   8  40 44
    7 2 J.Lulu    06/99 48317 green   9  24 26
    7 3 P.Bunny   02/99 48    Yellow  12 35 28
    7 4 J.Troll   07/99 4842  Brown-3 12 26 26
    7 5 L.Tansley 05/99 4712  Brown-2 12 30 28
    grade.txt
    [root@redhat script]#
    [root@redhat script]#awk '{if(NR>0 && $4~/Brown/) print $0}' grade.txt 
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#pwd
    /root/yzh/script
    [root@redhat script]#echo $PWD
    /root/yzh/script
    [root@redhat script]#echo $PWD | awk -F/ '{print $NF}'
    script
    [root@redhat script]#
    [root@redhat script]#echo "/usr/local/etc/rc.sybase" | awk -F/ '{print $NF}'
    rc.sybase
    [root@redhat script]#
    [root@redhat script]#awk '{name=$1;belts=$4; if(belts ~ /Yellow/) print name " is belt" belts}' grade.txt 
    P.Bunny is beltYellow
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '{name=$1;belts=$4; if(belts ~ /Yellow/) print name " is belt " belts}' grade.txt 
    P.Bunny is belt Yellow
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 < 27) print $0}' grade.txt 
    J.Lulu    06/99 48317 green   9  24 26
    J.Troll   07/99 4842  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#awk '{if($6 < "27") print $0}' grade.txt 
    J.Lulu    06/99 48317 green   9  24 26
    J.Troll   07/99 4842  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {BASELINE="27"} {if($6 < BASELINE) print $0}' grade.txt 
    J.Lulu    06/99 48317 green   9  24 26
    J.Troll   07/99 4842  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#awk '{if($1=="M.Tansley") $6=$6-1; print $1,$6,$7}' grade.txt 
    M.Tansley 39 44
    J.Lulu 24 26
    P.Bunny 35 28
    J.Troll 26 26
    L.Tansley 30 28
    [root@redhat script]#
    [root@redhat script]#awk '{if($1=="J.Troll") ($1="J.L.Troll"); print $1}'
    [root@redhat script]#awk '{if($1=="J.Troll") ($1="J.L.Troll"); print $1}' grade.txt 
    M.Tansley
    J.Lulu
    P.Bunny
    J.L.Troll
    L.Tansley
    [root@redhat script]#
    [root@redhat script]#awk '{if($1=="J.Troll") {$1="J.L.Troll";print $1}}' grade.txt 
    J.L.Troll
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "NAME Difference"} {if($6<$7) {$8=$7-$6;print $1,$8}} ' grade.txt 
    NAMEDifference
    M.Tansley 4
    J.Lulu 2
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "NAME Difference"} {if($6<$7) {diff=$7-$6;print $1,diff}} ' grade.txt 
    NAMEDifference
    M.Tansley 4
    J.Lulu 2
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '(tot+=$6); END {print "Club student total points: " tot}' grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    Club student total points: 155
    [root@redhat script]#
    [root@redhat script]#ls -l
    总用量 44
    -rw-r--r--  1 root root    1 2011/12/06 05:12:25 1
    -rwxrwxrwx  1 root root  103 2011/09/22 02:10:07 aa.bak
    -rwxr-xr-x  1 root root  121 2011/12/06 06:51:07 aa.sh
    drwxr-xr-x  2 root root 4096 2011/11/26 23:27:11 CD
    drwxr-xr-x  2 root root 4096 2011/12/06 10:29:51 Demo
    -rwxr-xr-x  1 root root   44 2011/09/22 12:11:21 first.sh
    -rwxrwxrwx  1 root root  195 2011/12/06 06:07:48 grade.txt
    -rwxrwxrwx  1 root root  303 2011/11/27 03:04:04 list
    -rwxrwxrwx  1 root root  101 2011/11/27 03:52:13 nameState
    -rwxrwxrwx  1 root root   44 2011/11/27 03:24:34 sedscr
    -rwxrwxrwx  1 root root   90 2011/09/21 20:39:22 test.sh
    [root@redhat script]#awk '/^[^d]/ {print $9" "$5} {tot+=$5} END {print "total KB:" tot}' grade.txt 
    8
    9
    12
    12
    12
    total KB:53
    [root@redhat script]#awk '/^[^d]/ {print $8" "$5} {tot+=$5} END {print "total KB:" tot}' grade.txt 
    8
    9
    12
    12
    12
    total KB:53
    [root@redhat script]#ls -l | awk '/^[^d]/ {print $8" "$5} {tot+=$5} END {print "total KB:" tot}'
    11
    aa.bak103
    aa.sh121
    first.sh44
    grade.txt195
    list303
    nameState101
    sedscr44
    test.sh90
    total KB:9194
    [root@redhat script]#ls -l | awk '/^[^d]/ {print $8"---------------------"$5} {tot+=$5} END {print "total KB:" tot}'
    ---------------------
    1---------------------1
    aa.bak---------------------103
    aa.sh---------------------121
    first.sh---------------------44
    grade.txt---------------------195
    list---------------------303
    nameState---------------------101
    sedscr---------------------44
    test.sh---------------------90
    total KB:9194
    [root@redhat script]#
    [root@redhat script]#awk 'gsub(/4842/,4899) {print $0}' grade.txt 
    J.Troll   07/99 4899  Brown-3 12 26 26
    [root@redhat script]#
    [root@redhat script]#cat grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print index("Bunny","ny")}' grade.txt 
    4
    [root@redhat script]#
    [root@redhat script]#awk '$1=="J.Troll" {print length($1) " " $1}' grade.txt 
    7 J.Troll
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print length("A FEW GOOD MEN")}'
    14
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print match("ANCD",/d/)}'
    0
    [root@redhat script]#awk 'BEGIN {print match("ANCD",/c/)}'
    0
    [root@redhat script]#awk 'BEGIN {print match("ANCD",/C/)}'
    3
    [root@redhat script]#awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt 
    4
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print split("123#456#678",myarray,"#")}'
    3
    [root@redhat script]#
    [root@redhat script]#awk '$1=="J.Troll" sub(/26/,"29",$0)' grade.txt 
    [root@redhat script]#
    You have new mail in /var/spool/mail/root
    [root@redhat script]#awk '$1=="J.Troll" sub(/26/,"29",$0)' grade.txt 
    [root@redhat script]#
    [root@redhat script]#cat 
    1          aa.bak     aa.sh      CD/        Demo/      first.sh   grade.txt  list       nameState  sedscr     test.sh    
    [root@redhat script]#cat grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    P.Bunny   02/99 48    Yellow  12 35 28
    J.Troll   07/99 4842  Brown-3 12 26 26
    L.Tansley 05/99 4712  Brown-2 12 30 28
    [root@redhat script]#awk '$1=="J.Troll" sub(/26/,"29",$0)' grade.txt 
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '{print substr($1,3)}' grade.txt 
    Tansley
    Lulu
    Bunny
    Troll
    Tansley
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {STR="A FEW GOOD MEN"} END {print substr(STR,7)}' grade.txt 
    GOOD MEN
    [root@redhat script]#
    [root@redhat script]#echo "Stand-by" | awk '{print length($0)}'
    8
    [root@redhat script]#
    [root@redhat script]#STR="mydoc.txt"
    [root@redhat script]#echo $STR | awk '{print substr($STR,7)}'
    txt
    [root@redhat script]#awk 'BEGIN {print " May Day May     104141171"}'
    MayDay
    May     Day
    [root@redhat script]#
    [root@redhat script]#echo "65" | awk '{printf "%c ",$0}'
    A
    [root@redhat script]#echo 'BEGIN {printf "%c ",65}'
    BEGIN {printf "%c ",65}
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {printf "%c ",65}'
    A
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {printf "%f ",999}'
    999.000000
    [root@redhat script]#
    [root@redhat script]#awk '{print "%-15s %s ",$1,$3}' grade.txt 
    %-15s %s
    M.Tansley 48311
    %-15s %s
    J.Lulu 48317
    %-15s %s
    P.Bunny 48
    %-15s %s
    J.Troll 4842
    %-15s %s
    L.Tansley 4712
    [root@redhat script]#
    [root@redhat script]#
    [root@redhat script]#awk '{print "%-5s %s ",$1,$3}' grade.txt 
    %-5s %s
    M.Tansley 48311
    %-5s %s
    J.Lulu 48317
    %-5s %s
    P.Bunny 48
    %-5s %s
    J.Troll 4842
    %-5s %s
    L.Tansley 4712
    [root@redhat script]#awk {print "%-15s %s ",$1,$3}' grade.txt 

    [root@redhat script]#awk '{print "%-15s %s ",$1,$3}' grade.txt 
    %-15s %s
    M.Tansley 48311
    %-15s %s
    J.Lulu 48317
    %-15s %s
    P.Bunny 48
    %-15s %s
    J.Troll 4842
    %-15s %s
    L.Tansley 4712
    [root@redhat script]#awk '{print "%s %s ",$1,$3}' grade.txt 
    %s %s
    M.Tansley 48311
    %s %s
    J.Lulu 48317
    %s %s
    P.Bunny 48
    %s %s
    J.Troll 4842
    %s %s
    L.Tansley 4712
    [root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "Name S.Number"} {printf "%-15s %s",$1,$3}' grade.txt 
    Name S.Number
    M.Tansley       48311J.Lulu          48317P.Bunny         48J.Troll         4842L.Tansley       4712[root@redhat script]#
    [root@redhat script]#awk 'BEGIN {print "Name S.Number"} {printf "%-15s %s ",$1,$3}' grade.txt 
    Name S.Number
    M.Tansley       48311
    J.Lulu          48317
    P.Bunny         48
    J.Troll         4842
    L.Tansley       4712
    [root@redhat script]#
    [root@redhat script]#awk '{if($5<AGE) print $0}' AGE=10  grade.txt 
    M.Tansley 05/99 48311 Green   8  40 44
    J.Lulu    06/99 48317 green   9  24 26
    [root@redhat script]#
    [root@redhat script]#df -k
    Filesystem             1K-块        已用     可用 已用% 挂载点
    /dev/mapper/VolGroup00-LogVol00
                           7063480   2786784   3917888  42% /
    /dev/sda1               101086     12826     83041  14% /boot
    none                    257720         0    257720   0% /dev/shm
    [root@redhat script]#
    [root@redhat script]#df -k | awk '($4 ~ /^[0-9]/) {if($4<TRIGGER) print $6" "$4}'
    [root@redhat script]#df -k | awk '($4 ~ /^[0-9]/) {if($4<TRIGGER) print $6" "$4}' TRIGGER=56000
    42%
    [root@redhat script]#
    [root@redhat script]#df -k | awk '($4 ~ /^[0-9]/) {if($4>TRIGGER) print $6" "$4}' TRIGGER=56000
    /boot83041
    /dev/shm257720
    [root@redhat script]#
    [root@redhat script]#who
    root     pts/0        Dec  6 09:54 (192.168.245.1)
    root     pts/1        Dec  6 10:29 (192.168.245.1)
    [root@redhat script]#who | awk '{print $1 " is logged on"}'
    root is logged on
    root is logged on
    [root@redhat script]#echo $LOGNAME 
    root
    [root@redhat script]#
    [root@redhat script]#who | awk '{if($1 == user) print $1 " you are connected to " $2}' user=$LOGNAME 
    root you are connected to pts/0
    root you are connected to pts/1   

  • 相关阅读:
    续上-选课系统
    第一个java web的课堂练习-开始
    子类与父类方法间的关系
    课后作业-3
    小结
    Java中子类与基类变量间的赋值
    今日总结
    今日总结
    今日总结
    今日总结
  • 原文地址:https://www.cnblogs.com/defias/p/3281716.html
Copyright © 2020-2023  润新知