定义: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. 二叉树和二叉查找树--数据结构与算法JavaScript描述(10)

    二叉树和二叉查找树 概念 树是一种非线性的数据结构,以分层的方式存储数据. 树被用来存储具有层级关系的数据,比如文件系统的文件: 树还被用来存储有序列表. 一棵树最上面的节点称为根节点. 如果一个节点 ...

  2. 图片验证码给AI使用

    为了破解图形验证码,AI需要大量的图片数据.为了简单获取大量的图形来喂给Ai模型训练,索性自己写一把.代码来一发..   import java.awt.Color; import java.awt. ...

  3. 20145202 2016-2017-2 《Java程序设计》第一周学习总结

    20145202 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 java是SUN公司推出的面相网络的编程语言. 特点:完全面向对象,与平台无关,跨平台性(例 ...

  4. Hadoop数据倾斜及解决办法

    数据倾斜:就是大量的相同key被partition分配到一个分区里,map /reduce程序执行时,reduce节点大部分执行完毕,但是有一个或者几个reduce节点运行很慢,导致整个程序的处理时间 ...

  5. HBase全网最佳学习资料汇总

    HBase全网最佳学习资料汇总 摘要: HBase这几年在国内使用的越来越广泛,在一定规模的企业中几乎是必备存储引擎,互联网企业阿里巴巴.百度.腾讯.京东.小米都有数千台的HBase集群,中国电信的话 ...

  6. windows系统下npm升级的正确姿势以及原理

    本文来自网易云社区 作者:陈观喜 网上关于npm升级很多方法多种多样,但是在windows系统下不是每种方法都会正确升级.其中在windows系统下主要的升级方法有以下三种: 首先最暴力的方法删掉no ...

  7. Delphi7目录结构----初学者参考

    打开Delphi的安装目录,如C:\Program Files\Borland\Delphi7,你将会看到目录下包含了一些文件和文件夹: ²        Source:存放的是Delpi提供的所有源 ...

  8. 软件测试面试题-适合零基础和工作多年的re

    软件测试面试题整理,可以看看:适合零基础和多年工作经验跳槽的人 有些问题会深挖,就不在整理了 详看图片:

  9. CentOS 6.5 下安装redis

    1.登录虚拟机后,直接输入命令:yum -y install redis 会出现一个错误: 是因为少了epel源, 2.运行:yum -y install epel-release 最后出现 Comp ...

  10. 转 Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5

    Using $.ajaxPrefilter() To Configure AJAX Requests In jQuery 1.5 Posted February 18, 2011 at 6:29 PM ...