zoukankan      html  css  js  c++  java
  • Awk basic and practice

    定义:Awk是一种程序语言,用来处理数据和产生报告。数据可来自标准输入,文件,管道输出。

    格式:#awk '/pattern/ {action}' filename

    术语:pattern, 样式是由正则表达式,或条件表达式,或两者共同的组合所构成的语句。

            action, 动作是在大括号内的一个或多个语句,且以“,”隔开。

    原理:样式可以和动作结合,也可以各自单独工作。样式控制大括号间的动作。

    Practice script

    # cat awk.sh 
    #/bin/bash
    
    echo '#print the contenct of employees'
    cat  employees;
    echo ''
    echo '#print line include eric'
    awk '/^eric/' employees 
    
    echo '#print $1,$2,$3,$4 in eric line'
    awk '/eric/ {print $1,$2,$3,$4}' employees
    
    echo '#print $1 $2 $3 $4 in eric line'
    awk '/eric/ {print $1 $2 $3 $4}' employees
    
    echo "#print date , month, year"
    date | awk '{print "month:" $2 "
    year:" $6}' 
    echo ""
    
    echo '#display error in /var/log/message'
    cat /var/log/messages | awk '/error/{print $3,$5,$6}'
    
    echo '#print two tab' 
    awk '/beijing/{print "		 How are you doing, "$1"!"}' employees
    
    echo '#print space' 
    awk '{print $1 " ID is " $3 "
    "}' employees
    
    echo '#print $0,and the number of record'
    awk '{print NR, $0 }' employees
    
    echo 'print $0,and the number of record'
    awk '{print $0, NR }' employees
    
    echo 'print $0,and the number of field'
    awk '{print $0, NF }' employees
    
    echo 'matching first line is frank, and show $1, $2'
    awk '$1 ~/frank/ {print NR,$1,$2}' employees
    
    echo 'matching first line is not frank, and show $1, $2'
    awk '$1 !~/frank/ {print NR,$1,$2}' employees
    
    echo 'matching line from frank to green, and show $1, $2'
    awk '/frank/,/green/ {print $1,$2}' employees
    
    echo '#print line3 bigger then 200000'
    cat employees | awk '$3>200000' 
    echo ""
    
    echo '#print number bigger then 200000'
    cat employees | awk '$3>200000 {print "they are: " $1}' 
    
    echo '#print line3 less then 200000'
    cat employees | awk '$3<200000' 
    echo ""
    
    echo '#print the result of $3*$4'
    awk '$1 ~/frank/ {salary=$3 * $4; print salary}' employees

    Output of the script

    # ./awk.sh 
    #print the contenct of employees
    eric    beijing   123456    10
    john    xian      849383    11
    mark    henan      939394    12
    frank    tokoyo      283098    2
    green    england      788888    3
    
    #print line include eric
    eric    beijing   123456    10
    #print $1,$2,$3,$4 in eric line
    eric beijing 123456 10
    #print $1 $2 $3 $4 in eric line
    ericbeijing12345610
    #print date , month, year
    month:Sep
    year:2013
    
    #display error in /var/log/message
    #print two tab
             How are you doing, eric!
    #print space
    eric ID is 123456
    
    john ID is 849383
    
    mark ID is 939394
    
    frank ID is 283098
    
    green ID is 788888
    
    #print $0,and the number of record
    1 eric    beijing   123456    10
    2 john    xian      849383    11
    3 mark    henan      939394    12
    4 frank    tokoyo      283098    2
    5 green    england      788888    3
    print $0,and the number of record
    eric    beijing   123456    10 1
    john    xian      849383    11 2
    mark    henan      939394    12 3
    frank    tokoyo      283098    2 4
    green    england      788888    3 5
    print $0,and the number of field
    eric    beijing   123456    10 4
    john    xian      849383    11 4
    mark    henan      939394    12 4
    frank    tokoyo      283098    2 4
    green    england      788888    3 4
    matching first line is frank, and show $1, $2
    4 frank tokoyo
    matching first line is not frank, and show $1, $2
    1 eric beijing
    2 john xian
    3 mark henan
    5 green england
    matching line from frank to green, and show $1, $2
    frank tokoyo
    green england
    #print line3 bigger then 200000
    john    xian      849383    11
    mark    henan      939394    12
    frank    tokoyo      283098    2
    green    england      788888    3
    
    #print number bigger then 200000
    they are: john
    they are: mark
    they are: frank
    they are: green
    #print line3 less then 200000
    eric    beijing   123456    10
    
    #print the result of $3*$4
    566196
  • 相关阅读:
    数据库之主表、从表、主键、外键
    eclipse编写js代码没有提示
    思维导图xmind的使用方法
    整理一下Apache与Tomcat的关系
    全栈开发者,一个很好的自学编程网站
    svn文件被锁不能提交的解决办法
    在SQL Server数据库中执行存储过程很快,在c#中调用很慢的问题
    php安装redis扩展
    PHP点击按钮拷贝
    PHP文件下载
  • 原文地址:https://www.cnblogs.com/oskb/p/3326219.html
Copyright © 2011-2022 走看看