定义: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. [Cracking the Coding Interview] 4.3 List of Depths

    Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth. ...

  2. es同步mysql同步-logstash

    1.下载es https://www.elastic.co/downloads/elasticsearch 修改 config 下elasticsearch.yml   ip和端口等配置 2.下载ki ...

  3. webpack实践总结

    一.Loader写法及执行顺序 从webpack2起,loader的格式如下: module: { rules: [ {test: /\.css$/, use: ['style-loader','cs ...

  4. fiddler抓包-简单易操作(一)

    1.下载fiddler 可以到fiddler官网去下,网址:https://www.telerik.com/download/fiddler 下载完成后,安装即可. 2.运行fiddler,进入fid ...

  5. static 关键字解析(转)

    static关键字解析   Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面 ...

  6. BZOJ 2597 剪刀石头布(最小费用最大流)(WC2007)

    Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道 ...

  7. PHP 用Symfony VarDumper Component 调试

    Symfony VarDumper 类似 php var_dump() 官方文档写的安装方法 : 按照步骤 就可以在 running any PHP code  时候使用了 In order to h ...

  8. IDE API SDK JDK

    一.IDE 英文全称:Integrated Development Environment 中文名称:集成开发环境 本质:应用程序 功能:提供程序开发环境 组成:代码编辑器.编译器.调试器.图形用户界 ...

  9. Z.XML-Cocos2d-x开发笔记

    大家都在热火朝天的使用Cocos2d-x引擎做游戏开发,那么大家不妨把过程中解决的关键问题记录在这里,做一个分享! 1.在Android平台下打开网页 1.1修改项目工程源文件 在你的项目工程源文件中 ...

  10. Linux arm64的虚拟内存布局

    原创翻译,转载请注明出处. 页表转换arm64在硬件体系结构上支持4级的每页大小为4K的页表转换,也支持3级的页大小64KB的页表转换.在linux arm64中,如果页的大小为4KB,使用3级页表转 ...