zoukankan      html  css  js  c++  java
  • (转)awk实例练习(二)

    文章转自 http://www.cnblogs.com/zhuyp1015/archive/2012/07/14/2591842.html

    先来总结一下awk内置变量:

    ARGC          命令行参数个数
    ARGV          命令行参数排列
    ENVIRON       支持队列中系统环境变量的使用
    FILENAME      awk浏览文件名
    FNR           浏览文件的记录数
    FS            设置输入域分隔符,等价于命令行-F选项
    NF            浏览记录的域个数
    NR            已读的记录数
    OFS           输出域分隔符
    ORS           输出例句分隔符
    RS            控制记录分隔符
     
     
    $ 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
     
    #使用 -F 参数指定分隔符
    $ echo $PWD
    /home/zhuyupeng
     
    $ echo $PWD | awk -F/ '{print $NF" "NF}'
    zhuyupeng       3
     
    #设置变量名,将27 赋值给变量BASELINE
    $ awk 'BEGIN {BASELINE="27"} $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
     
    #修改数值域取值,注意‘{}’
    $ 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
     
    #修改文本域取值
    $ awk '{if($1=="J.Troll") $1="J.L.Troll"; print $1}' grade.txt
    M.Tansley
    J.Lulu
    P.Bunny
    J.L.Troll
    L.Tansley
     
    #创建新的输出域,这里新的输出域为 diff
    $ awk 'BEGIN {print "Name Difference"} {if($6<$7) {diff=$7-$6; print $1,diff}}' grade.txt
    Name     Difference
    M.Tansley 4
    J.Lulu 2
     
    #统计某一个域的和,使用‘+=’ 下面的例子统计第六个域的和
    $ 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
     
    #注意区别,加‘{}’则不打印文件
    $ awk '{(tot+=$6)}; END{print "Club student total points: " tot}' grade.txt
    Club student total points: 155
     
     
    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 'gsub(/4842/,4899) {print $0}' grade.txt
    J.Troll 07/99   4899    Brown-3 12      26      26
     
    #查询字符串第一次出现的位置,注意使用BEGIN,否则每一行都会打印,字符串使用引号括起来
    $ awk 'BEGIN{print index("Bunny","ny")}' grade.txt
    4
     
    #长度
    $ awk '$1=="J.Troll" {print length($1)" "$1}' grade.txt
    7 J.Troll
     
    #match 使用: 找不到返回0,找到返模式串在匹配串中的位置
    #注:单独使用 加BEGIN
    $ awk 'BEGIN {print match("ANCD",/d/)}'
    0
     
    #以下两种模式都正确
    $ awk '$1=="J.Lulu" {print match($1,"u")}' grade.txt
    4
     
    $ awk '$1=="J.Lulu" {print match($1,/u/)}' grade.txt
    4
     
    #split 返回字符串数组元素个数
    $ awk 'BEGIN {print split("123#456#789",myarray,"#");print myarray[1],myarray[2],myarray[3]}'
    3
    123 456 789
     
    #sub,发现并替换模式的第一个位置
    $ awk '$1=="J.Troll" {sub(26,29,$0)} {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      29      26
    L.Tansley       05/99   4712    Brown-2 12      30      28
     
    #substr,返回字符串指定范围内的子串
    $ awk '$1=="L.Tansley" {print substr($1,1,5)}' grade.txt
    L.Tan
     
    #使用substr返回指定位置开始的后缀部分,范围只给了一个参数,注意和上一个例子相对比
    $ awk '{print substr($1,3)}' grade.txt
    Tansley
    Lulu
    Bunny
    Troll
    Tansley
     
    #从shell中向awk传递字符串,通过 echo 加管道的方式
    $ echo "Test" | awk '{print length($0)}'
    4
    $ STR="mydoc.txt"
    $ echo $STR | awk '{print substr($STR,7)}'
    txt
     
    Coding Life
  • 相关阅读:
    怎么将一个类的成员函数作为指针传递给另一个类的成员函数 沉沉_
    C/C++中的头文件多文件组织 沉沉_
    函数的返回值返回变量和引用 沉沉_
    多操作赋的语义判断(如 int& *a和int* &a) 沉沉_
    ctags: 提示错误ctags: unrecognized option 'format=2' 沉沉_
    C++中关于流的概念 沉沉_
    Verilog 初学笔记顺序操作 和 并行操作的一点思考(参考黑金教程:Verilog HDL那些事 建模篇) 沉沉_
    void 指针 (转载) 沉沉_
    C语言的体系结构main函数存在的必然性(听杨力祥老师的课) 沉沉_
    转载:VC6.0中容易遇到的错误。http://hi.baidu.com/%C8%FD%C9%EE/blog/item/4a756ff2cb6bdb19b07ec5df.html 沉沉_
  • 原文地址:https://www.cnblogs.com/lewiskyo/p/4605295.html
Copyright © 2011-2022 走看看