定义: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. Python3 模块、包调用&路径

    ''' 以下代码均为讲解,不能实际操作 ''' ''' 博客园 Infi_chu ''' ''' 模块的优点: 1.高可维护性 2.可以大大减少编写的代码量 模块一共有三种: 1.Python标准库 ...

  2. ubuntu配置机器学习环境(四) 安装intel MKL

    在这一模块可以选择(ATLAS,MKL或者OpenBLAS),我这里使用MKL,首先下载并安装英特尔® 数学内核库 Linux* 版MKL,下载链接, 请下载Student版,先申请,然后会立马收到一 ...

  3. 使用 MySQL 存储 Hue 元数据

    1.在 MySQL 中增加数据库 hue 2.编辑 hue.ini 文件 [[database]] # Database engine is typically one of: # postgresq ...

  4. php复制目录很浪

    一不小心搞出个超级深层次文件夹 主要是因为懒,在网上随便找了段复制文件夹的代码贴上了,结果是很恐怖,一个文件夹复制到他自身里面的时候,将会产生循环嵌套文件夹,后果是,windows因为文件名太长而无法 ...

  5. 第5模块闯关Bootstrap

    “行(row)”必须包含在 .container (固定宽度)或 .container-fluid (100% 宽度)中,以便为其赋予合适的排列(aligment)和内补(padding). 通过“行 ...

  6. C++各种类型的简单排序大汇总~

    啊,排序的技能点也太多了吧!!!LITTLESUN快要**在排序的技能场了啊!(划掉)经历了两天48小时2880分钟172800秒的艰苦奋斗,终于终于终于学的差不多了!明天就可以去打排序的小怪喽!(撒 ...

  7. javascript-es6学习笔记

    es6技术培训文档 第一阶段:1.let与const用法2.变量的解构赋值3.字符串的扩展4.正则的扩展5.数组的扩展6.函数的扩展7.对象的扩展8.Symbol9.Set和Map数据结构 第二阶段: ...

  8. [转]Git,SVN的优缺点及适合的范围,开源项目?公司项目?

    使用git不久,粗浅理解: 1)适用对象不同.Git适用于参与开源项目的开发者.他们由于水平高,更在乎的是效率而不是易用性.Svn则不同,它适合普通的公司开发团队.使用起来更加容易. 2)使用的场合不 ...

  9. Python-学习-项目1-即时标记-1

    买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习. 项目名字:即时标记 大致的意思就是把一个纯文本文件标记成自己想要的格式文件. 首先就是待处理文本,我找不到 ...

  10. python 网络编程(远程执行命令与粘包)

    远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...