高效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 [email protected]    F

Anthony  555-3412 [email protected]    A

Bill            555-7685 [email protected]            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")

时间: 2024-08-12 19:04:08

高效awk编程第四版学习笔记的相关文章

算法第四版学习笔记之快速排序 QuickSort

软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:

算法(第四版)学习笔记之java实现选择排序

选择排序步骤: 1.找到数组中参与遍历比较的所有元素中的最小元素的下标: 2.将最小元素与数组中参与遍历比较的第一个元素进行交换(如果第一个元素就是最小元素的话,那么也会进行一次交换): 3.若数组中还有需要参与遍历比较的元素,则跳转到步骤1:否则排序结束. 在算法第四版中给出的所有排序均是适用于任意实现了Comparable接口的数据类型,若要将数字作为测试用例,请勿使用基本数据类型,改用Integer等实现了Comparable接口的对象. 选择排序代码如下: /** * * @author

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记四)之Operators

At the lowest level, data in Java is manipulated using operators Using Java Operators An operator takes one or more argument and produces a new value. The arguements are in a different form than ordinary method calls, but the effect is the same. + :

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

The genesis of the computer revolution was a machine. The genesis of out programming languages thus tends to look like that machine. 计算机革命起源于机器,因此编程语言的产生也始于对机器的模仿 Computers are mind amplification tools and a different kind of expressive medium. 计算机是大

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation for the String class, you’ll see that every method in the class that appears to modify a String actually creates and returns a brand new String object c

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization &amp; Cleanup

Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> running out of resources (most notably, memory) Java adopted the constructor, and in addition has a garbage collector that automoatically releases memory res

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

Polymorphism is the third essential feature of an object-oriented programming language,after data abastraction and inheritance. It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allo

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Reusing Classes

The trick is to use the classes without soiling the existing code. 1. composition--simply create objects of your existing class inside the new class. simply reusing the functionality of the code, not its form 2.inheritance--creates a new class as a t