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 "\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的更多相关文章
- PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642
PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642 题目描述: 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一 ...
- PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...
- PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642
PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...
- PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...
- Grep basic and practice
定义:Grep (Globally search for the reqular expression and print out the line). 好处:Grep 在执行时不需要先调用编辑程序, ...
- PAT (Basic Level) Practice (中文)1057 数零壹 (20 分) (按行输入带空格的字符串)
给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...
- 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 和 ...
随机推荐
- UVA - 12230
#include <bits/stdc++.h> using namespace std; int n; double d; double p,l,v,ret,sum; ; /* 村庄A, ...
- Django调试models输出的SQL语句
django1.3在shell下,调试models变得更为简单了,不用像之前的版本,手工去调用django query,才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings. ...
- codevs 1214 线段覆盖/1643 线段覆盖 3
1214 线段覆盖/1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定x轴上的N(0< ...
- Python 3基础教程31-urllib模块
本文介绍Python里的urllib模块,这个urllib主要处理web服务的,如果需要做接口测试,或者写Python的网络爬虫,这个urllib就是最底层的库.需要用到里面的请求方法等. 1. 先看 ...
- Struts2(三.用户登录状态显示及Struts2标签)
1.编写main.jsp /WebContent/main.jsp 之前用户登录时已把用户存入session <%@ page language="java" content ...
- (原创)不过如此的 DFS 深度优先遍历
DFS 深度优先遍历 DFS算法用于遍历图结构,旨在遍历每一个结点,顾名思义,这种方法把遍历的重点放在深度上,什么意思呢?就是在访问过的结点做标记的前提下,一条路走到天黑,我们都知道当每一个结点都有很 ...
- MATLAB中矢量场图的绘制 (quiver/quiver3/dfield/pplane) Plot the vector field with MATLAB
1.quiver函数 一般用于绘制二维矢量场图,函数调用方法如下: quiver(x,y,u,v) 该函数展示了点(x,y)对应的的矢量(u,v).其中,x的长度要求等于u.v的列数,y的长度要求等于 ...
- java计算两个日期之间的相隔天数
[原创] 之前在很多竞赛的题目中有这样饿一类题,计算给定两个日期之间的相隔天数,或者很多类似的题目,都是需要转化到这个问题上时,之前用c++写的时候思想是这样的,一个结构体,包括年月日,还有一个计算下 ...
- wutianqi 博客 母函数
母函数(Generating function)详解 — Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...
- Spring Data学习(一):初识
目录 前言 添加Spring Data 配置pom.xml 配置数据库相关信息(application.properties) 配置数据库信息 配置自动根据实体类在数据库创建表 创建User.java ...