定义: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 "\nyear:" $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 "\t\t How are you doing, "$1"!"}' employees echo '#print space'
awk '{print $1 " ID is " $3 "\n"}' 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
john xian
mark henan
frank tokoyo
green england #print line include eric
eric beijing
#print $,$,$,$ in eric line
eric beijing
#print $ $ $ $ in eric line
ericbeijing12345610
#print date , month, year
month:Sep
year: #display error in /var/log/message
#print two tab
How are you doing, eric!
#print space
eric ID is john ID is mark ID is frank ID is green ID is #print $,and the number of record
eric beijing
john xian
mark henan
frank tokoyo
green england
print $,and the number of record
eric beijing
john xian
mark henan
frank tokoyo
green england
print $,and the number of field
eric beijing
john xian
mark henan
frank tokoyo
green england
matching first line is frank, and show $, $
frank tokoyo
matching first line is not frank, and show $, $
eric beijing
john xian
mark henan
green england
matching line from frank to green, and show $, $
frank tokoyo
green england
#print line3 bigger then
john xian
mark henan
frank tokoyo
green england #print number bigger then
they are: john
they are: mark
they are: frank
they are: green
#print line3 less then
eric beijing #print the result of $*$

Awk basic and practice的更多相关文章

  1. PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642 题目描述: 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一 ...

  2. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  3. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  4. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  5. PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...

  6. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...

  7. Grep basic and practice

    定义:Grep (Globally search for the reqular expression and print out the line). 好处:Grep 在执行时不需要先调用编辑程序, ...

  8. PAT (Basic Level) Practice (中文)1057 数零壹 (20 分) (按行输入带空格的字符串)

    给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...

  9. PAT (Basic Level) Practice (中文)1022 D进制的A+B

    1022 D进制的A+B 输入两个非负 10 进制整数 A 和 B (≤2^30^−1),输出 A+B 的 D (1<D≤10)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 ...

随机推荐

  1. 小程序开发-7-访问api数据与ES6在小程序中的应用

    访问API数据与ES6在小程序中的应用 看待组件的两种观点 组件复用 代码分离-(特别重要) 不能在一个页面写所有的代码,代码分离具有很强的可读性.可维护性 Blink Api 介绍与测试API ur ...

  2. 动态规划(DP)算法

    参考https://blog.csdn.net/libosbo/article/details/80038549 动态规划是求解决策过程最优化的数学方法.利用各个阶段之间的关系,逐个求解,最终求得全局 ...

  3. SEARCH(文字の検索)

    文字列に関する無効命令 以下の各命令は無効であり.4.6 および 6.10 までのリリースとの互換性を確保するためにのみ利用可能となっています.これらの命令が古いプログラムの中に出現することはあります ...

  4. ORA-12705: Cannot access NLS data files or invalid

    RedHat7.1 Oracle11gr2 oracle 默认的编码方式如下:SQL> select userenv('language') from dual; USERENV('LANGUA ...

  5. leetcode笔记--6 Add Digits

    question: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  6. 链接程序的时候遇到问题:fatal error LNK1104: cannot open file 'rctrl-d.lib'

    1.lib库文件没有添加到工程中(工程里面根本就没有这个文件) 2.

  7. html基础问题总结

    1.reflow 在CSS规范中有一个渲染对象的概念,通常用一个盒子(box, rectangle)来表示.mozilla通过一个叫frame的对象对盒子进行操作.frame主要的动作有三个: 构造f ...

  8. Windows自带的磁盘填充命令

    一张不用了的SD卡要给别人,之前一直是手机使用的,担心有一些资料被恢复,想要将它内容清空.以前就知道数字公司有一个磁盘填充的工具,后来网上搜一搜发现Windows有一个自带的命令用于磁盘填充. 首先进 ...

  9. 05-Mysql数据库----补充内容

    数据库命名规则: 数据库命名规则: 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使用关键字如 create select 不能单独使用数字 最长128位 # 基本上跟python或者 ...

  10. C语言运算符(注意事项)

    1.C语言取余注意事项:%   a.求余.模运算符(%)时要求两数必须是整型数据. b.取余的结果,是取决于被除数   (不管除数是正数 还是 负数,模的符号与被除数的符号相同).   例:8÷2=4 ...