高效awk编程第四版学习笔记

简介:

How to Run awk Programs

语法:

awk 'program' input-file1 input-file2 适用于短program

awk -f program-file input-file1 input-file2 用在长program

awk是输入驱动的,也就是说没有输入就结束

awk模型:把输入的每一行进行检查是否满足pattern,如果满足就执行action,如果不满足,下一行处理,直到文件的末尾。另外一种模式就是BEGIN和END,这两个在主循环体内只执行一次。

Running Long Programs时

awk -f source-file input-file1 input-file2

eg:BEGIN { print "Don't Panic!" }将此语句写入advice文件,然后执行awk -f advice

does the same as this one: awk 'BEGIN { print "Don\47t Panic!" }'

Exec awk Programs

使用文件写programs时,.awk文件要写明"#!" like this file:

#! /bin/awk -f 

BEGIN { print "Don't Panic!" }


$chmod +x advice

$advice

Don't Panic!

如果$PATH没有也就是直接敲advice不行,你就需要./advice执行

awk is an interpreted language so used "#!"


Comments in awk Programs 在awk中注释 # like this

# This program prints a nice,friendly message. It helps

# keep novice users from begin afraid of the computer.

BEGIN { print "Don't Panic!" }

但是写在短program后面的注释就有语法错误 eg: awk 'BEGIN { print "hello" } #let's be cute' 


shell quoting issues 单引号开始结束'',里边的显示用双引号"",如果使用了双引号开始与结束,那么里边的print显示要用\"转义双引号,like this

awk 'BEGIN { print "Don\47t Panic!"}'

awk "BEGIN { print \"Don't Panic!\"}"


FS note 以及多重引号问题

awk -FS"" '{ print "hello" } wrong

awk -FS "" '{ print "hello" } right


Amelia    555-5553 amelia.zodiacuseque@gmail.com    F

Anthony  555-3412 anthony.asserturo@hotmail.com    A

Bill            555-7685 bill.drowning@hotmail.com            A


Jan    13    25    15    115

Feb    15    32    24    226

Mar    15    24    34    228

Apr    31    52    63    420

匹配li并显示 

模式匹配即parttern有以下几种:

正则表达式 /^r/

表达式 length($0) >100 $6 == "Nov"

匹配$0 ~ "a"

awk '/li/{print $0}' mail-list

awk 'length($0) >80' data 默认awk不写print也会执行print $0

awk '{ if(length($0)  >max) max = length($0)}END{print max}' date 打印最大长度的变量

awk '{ if(x <length($0)) x=length($0)} END{print mix}' data 打印最小的那个 

awk 'NF > 0' data  打印每一行

awk 'BEGIN { for (i=1;i<=7;i++) print int(101*rand())}' 打印7个0-100随机数

ls -l | awk '{x+=$5} END{print "total K-bytes : " x/1024}' 打印总字节数

awk -F ":" '{ print $1}' /etc/passwd | sort 打印排序后用户名

awk 'END{print NR}' data 打印文件总行数区别于FNR

awk 'NR%2==0' data打印偶数行

以上都是单一规则rules

awk '/12/ {print $0};/21/{print $0}' mail-list inventory-shipped

有两个规则,那么如果一条数据既满足12又满足21,那么就会显示两条一样数据

A More Complex Example 

ls -l | awk '$6 == "Nov" {sum+=5;} END{print sum}' 


调用awk

awk [options] -f progfile [--] file ...

awk [options] [--] 'program' file ...

标准参数

-F --field-separator

-f --source-file

-v --var=val

读取标准输入和其他文件的时候用,用“-”参数like this:

some_command | awk -f myprog.awk file1 - file2

先读取file1 然后读取some_command的输入 再读取file2

http://www.iqiyi.com/v_19rrlyojhc.html?vfm=m_332_bing引入其他的文件  @include  "filename"


namely test1 and test2

test1 script:

BEGIN { print "This is script test1" }

and here is test2:

@include "test1"

BEGIN { print "This is script test2." }

$gawk -f test2

This is script test1

This is script test2

How to Use 有规律的表达式

语法:exp ~ /regexp/

awk '$1 ~ /J/' inventory-shipped 第一个字段匹配“J”

like this :  awk '{ if ($1 ~ /J/) print }' inventory-shipped

语法:exp !~ /regexp/

awk '$1 !~ /J/' inventory-shipped 第一个字段不匹配“J”

Escape Sequences 在输出 ” 跟 \ 时候一定要用 \ 进行转义

必备一些正则知识,...{n,m} + * ? |

-F 指定这些为分隔符时用 \进行转义 \ ] - ^ put a '\' in front of it

eg: [d\]] 指定以d分割或者]分割


echo 'xxAA xxBxx C' | gawk -F '(^x+)|( +)' '{for (i=1;i<=NF;i++) printf "--->%s<--",$i}'

指定多个分隔符以后,首先判断空格,如果没有再以表达式为准,以设置的最多为准,输出

--><--

-->AA<--

-->xxBBxx<--

-->C<--

OFS ORS FS RS

print items >> output-file

print items | command

print items |  & command

awk '{ print $1 > "names.unsorted" 

command = "sort -r > names.sorted"

print $1 | command }' mail-list


/dev/stdin The standard input (file descriptor 0)

/dev/stdout The standard output (file descriptor 1)

/dev/stderr The standard error output (file descriptor 2)



close(filename)

close(command)

不能获取以上两个函数的返回值,如果获取会报错

"sort -r names" | getline foo 

then you must close it with this:

close("sort -r names")


本文转自 aklaus 51CTO博客,原文链接:http://blog.51cto.com/aklaus/1844722

相关文章
|
3月前
|
存储 Shell 程序员
Python 进阶指南(编程轻松进阶):七、编程术语
Python 进阶指南(编程轻松进阶):七、编程术语
41 0
|
3月前
|
存储 Shell Go
使用 Python 创造你自己的计算机游戏(游戏编程快速上手)第四版:第五章到第九章
使用 Python 创造你自己的计算机游戏(游戏编程快速上手)第四版:第五章到第九章
110 0
|
4月前
|
存储 编译器 程序员
学习C语言必备的基础知识详解
学习C语言的第一步,肯定是要先去学习了解一下相关的概念和符号,我们写的代码就是由一堆规定好的有特殊含义的符号组成的。C语言的数据类型细分出来会有很多种,每种数据类型占内存大小都不同,对于刚接触编程语言的人来说,确实很让人头疼。其实存在这么多的类型,是为了能够更加丰富的表达生活中的各种值。比如钱,既有面额100元、50元、10元的,也有1元、1角甚至是1分的。如果我们手里有一张百元大钞,去商店购物看到有一件打折后卖98元的衣服,那我们可以直接用手里的一百元去付钱就行了,但是当你看到了儿时很喜欢吃的糖,还是卖1角两个,这个时候你再拿着100去买,就有点不合适了。对于数据存放在内存中也是同样的道理。
167 1
学习C语言必备的基础知识详解
|
11月前
|
存储 Java 编译器
一起啃书(C Primer Plus 第六版)--C语言概述&lt;附大量编程题&gt;
一起啃书(C Primer Plus 第六版)--C语言概述&lt;附大量编程题&gt;
112 0
|
SQL 算法 Java
【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
硬件工程师近年来也开始慢慢吃香,校招进大厂年薪总包不下30-40w的人数一大把!而且大厂人数并没有饱和!
【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门
|
存储 机器学习/深度学习 编译器
Python编程基础:文件基础
文件操作作为Python基础知识的一部分也非常的重要,熟练的掌握文件的各项操作,将来我们在数据分析、机器学习等一系列领域中才能更好的进行数据处理。
138 0
Python编程基础:文件基础
|
移动开发 运维 网络协议
Linux面试必考-高级文本处理工具进阶| 学习笔记
快速学习Linux面试必考-高级文本处理工具进阶
130 0
|
运维 Java Shell
Shell脚本编程基础|学习笔记
快速学习Shell脚本编程基础|学习笔记
108 0

热门文章

最新文章