• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

awk笔记

2020-05-17

1. 概要

文本和数据进行处理的编程语言

awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理


1
2
awk [options] 'script' var=value file(s)
awk [options] -f scriptfile var=value file(s)
  • -v var=value 赋值一个用户定义变量,将外部变量传递给awk
  • -f scripfile 从脚本文件中读取awk命令

很多UNIX工具都会生成行和列信息,AWK是处理这些行和列的优秀工具,而且比大多数传统的编程语言更容易使用。

它可以被认为是一个伪C语言的解释器,因为它能理解与C语言相同的算术运算符,AWK还具有字符串操作功能,所以它可以搜索特定的字符串并修改输出。

AWK还具有关联数组,这一点非常有用,也是大多数计算语言所缺乏的功能

“AWK “是由该语言的三位开发者的首字母缩写而来。A. Aho,B. W. Kernighan和P. Weinberger

2. 模式和操作

awk脚本是由模式和操作组成的

  • pattern { action }

2.1. 模式(pattern)

模式指定了动作的执行时间

  • /正则表达式/:使用通配符的扩展集
  • 关系表达式:使用运算符进行操作,可以是字符串或数字的比较测试
  • 模式匹配表达式:用运算符(匹配)和!(不匹配)
  • BEGIN语句块、pattern语句块、END语句块

2.2. 操作(action)

  • 变量或数组赋值
  • 输出命令
  • 内置函数
  • 控制流语句

2.3. 脚本基本结构

1
$ awk 'BEGIN{ print "start" } pattern{ commands } END{ print "end" }' file
  • 个awk脚本通常由:BEGIN语句块、能够使用模式匹配的通用语句块、END语句块3部分组成,这三个部分是可选的
  • 脚本通常是在 单引号 或 双引号中

There are two differences between AWK and a shell processing the characters within double quotes.

  • AWK understands special characters follow the “" character like “t”. The Bourne and C UNIX shells do not.
  • Also, unlike the shell (and PERL) AWK does not evaluate variables within strings. To explain, the second line could not be written like this:{print "$8\t$3" }.That example would print “$8 $3”.
    • Inside the quotes, the dollar sign is not a special character.Outside, it correspxonds to a field.x
      • Awk is diffxerent. The dollar sign means txhat we are refering to a fieldx or column in the current linex
        x

3. 工作原理x

  • 执行BEGIN{ commands }语句块中的语句
    • 比如变量初始化、打印输出表格的表头等
  • 从文件或标准输入(stdin)读取一行,然后执行pattern{ commands }语句块;从第一行到最后一行重复这个过程,直到文件全部被读取完毕
    • 如果没有提供pattern语句块,则默认执行{ print },即打印每一个读取到的行
  • 执行END{ commands }语句块
    • 比如打印所有行的分析结果这类信息汇总
  • 执行方式
    • chmode +x file.awk && awk -f file.awk
      • #!/bin/awk -f
    • chmode +x file.sh && ./file.sh

3.1. print

Like most UNIX utilities, AWK is line oriented. That is, the pattern specifies a test that is performed with each line read as input.

  • 当使用不带参数的print时,它就打印当前行
  • 在awk的print语句块中双引号是被当作拼接符使用
    1
    2
    $ echo | awk '{ var1="v1"; var2="v2"; var3="v3"; print var1"="var2"="var3; }'
    v1=v2=v3

3.2. c shell

如果AWK脚本的每一行不是脚本的最后一行,则必须有一个反斜线。这是必要的,因为C shell默认情况下不允许字符串超过一行

4. 动态变量

In many computer languages, a string has a start quote, and end quote, and the contents in between. If you want to include a special character inside the quote, you must prevent the character from having the typical meaning. In the C language, this is down by putting a backslash before the character.

In the C and Bourne shell, the quote is just a switch. It turns the interpretation mode on or off. There is really no such concept as “start of string” and “end of string”.

The quote character is not passed on to the application

4.1. 默认参数

The Bourne shell has a mechanism to provide a value for a variable if the value isn’t set, or is set and the value is an empty string

1
2
3
4
${variable:-defaultvalue}

#!/bin/sh
awk '{print $'"${1:-1}"'}' # 读第一个参数($1),默认是1

5. 运算

ExpressionResult
7+310
7-34
7*321
7/32.33333
7%31
7 373
  • AWK还支持C语言中的 “++”和”–”运算符

6. 其他

awk vs cut

  • What are the exact differences between awk and cut with grep? - Unix & Linux Stack Exchange

7. 参考

  • awk 命令,Linux awk 命令详解:文本和数据进行处理的编程语言 - Linux 命令搜索引擎
  • Awk - A Tutorial and Introduction - by Bruce Barnett
  • Shell
  • Operating System
  • Linux
  • Basic
Flask浅析-1
shell脚本编程基础
  1. 1. 1. 概要
  2. 2. 2. 模式和操作
    1. 2.1. 2.1. 模式(pattern)
    2. 2.2. 2.2. 操作(action)
    3. 2.3. 2.3. 脚本基本结构
  3. 3. 3. 工作原理x
    1. 3.1. 3.1. print
    2. 3.2. 3.2. c shell
  4. 4. 4. 动态变量
    1. 4.1. 4.1. 默认参数
  5. 5. 5. 运算
  6. 6. 6. 其他
  7. 7. 7. 参考
© 2024 何决云 载入天数...