AWK使用小结

简介

这里写图片描述

AWK是贝尔实验室1977年搞出来的文本出现神器,可谓是上古年代的神器了。
之所以叫AWK是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的Family Name的首字符。要学AWK,就得提一提AWK的一本相当经典的书《The AWK Programming Language》,它在豆瓣上的评分是9.4分!在亚马逊上居然卖1022.30元。

是一种强大的文本处理工具
具有自己独立的处理语法结构语言

注: 有一本相当经典的书《The AWK Programming Language》,可以下载PDF来看看。
另外,还发现一个在线的中文版本

这里不想全面介绍AWK的用法,而是以解决实际问题为目标,介绍AWK的一种常见用法。

另眼看awk

统观awk的用法,似乎有些眼熟,不错,那就是与办公软件Excel的功能是相同的,只是这个是命令方式,是终端时代的产物,在现如今仍不落后,关键时候仍能大显身手,力挽狂澜!

awk可以看作是一个功能强大,能通过结构化编程语言与它沟通的现代化工具,它用来处理文本数据,以行为基本的读取单位,可将每行分成若干字段(默认以空格分割)。支持为它添加有条件的操作处理过程。

内建变量

内建变量
说到了内建变量,我们可以来看看awk的一些内建变量:

变量名变量描述
$0当前记录(这个变量中存放着整个行的内容)
1  n当前记录的第n个字段,字段间由FS分隔
FS输入字段分隔符 默认是空格或Tab
NF当前记录中的字段个数,就是有多少列
NR已经读出的记录数,就是行号,从1开始,如果有多个文件话,这个值也是不断累加中。
FNR当前记录数,与NR不同的是,这个值会是各个文件自己的行号
RS输入的记录分隔符, 默认为换行符
OFS输出字段分隔符, 默认也是空格
ORS输出的记录分隔符,默认为换行符
FILENAME当前输入文件的名字

基本用法

格式为:模式 - 动作 序列

pattern    { action }
pattern    { action }
...

注:
- 上述序列通常使用单引号括起来
- 某些语句中,可能没有模式;
- -另一些语句中,可能没有动作及其大括号。
- awk检查你的程序以确认不存在语法错误后,一次读取一行输入,并对每一行按序处理模式。
- 对于每个匹配到当前输入行的模式,执行其关联的动作。
- 不存在模式,则匹配每个输入行,因此没有模式的每个动作对于每个输入行都要执行
- 一个仅包含模式的模式-动作语句将打印匹配该模式的每个输入行。

执行方式

命令行方式

awk [-F field-separator] ‘commands’ input-file(s)
其中,commands 是真正awk命令,[-F域分隔符]是可选的。 input-file(s) 是待处理的文件。
在awk中,文件的每一行中,由域分隔符分开的每一项称为一个域。通常,在不指名-F域分隔符的情况下,默认的域分隔符是空格。

shell脚本方式

将所有的awk命令插入一个文件,并使awk程序可执行,然后awk命令解释器作为脚本的首行,一遍通过键入脚本名称来调用。
相当于shell脚本首行的:#!/bin/sh
可以换成:#!/bin/awk

将所有的awk命令插入一个单独文件,然后调用:

awk -f awk-script-file input-file(s)
其中,-f选项加载awk-script-file中的awk脚本,input-file(s)跟上面的是一样的。

小实例

问题:找到某个内核版本修改过哪些文件(只需要提供文件名和路径)

问题分析:
1. 先通过生成Patch的方式,找到两个版本之间的变更
2. 对Patch文件进行分析处理,即awk上阵工作,过滤出变更的文件名及路径信息。

每个Patch文件的形如下:

diff --git a/Documentation/devicetree/bindings/crypto/samsung-sss.txt b/Documentation/devicetree/bindings/crypto/samsung-sss.txt
index a6dafa83c6df..7a5ca56683cc 100644
--- a/Documentation/devicetree/bindings/crypto/samsung-sss.txt
+++ b/Documentation/devicetree/bindings/crypto/samsung-sss.txt
@@ -23,10 +23,8 @@ Required properties:
   - "samsung,exynos4210-secss" for Exynos4210, Exynos4212, Exynos4412, Exynos5250,
        Exynos5260 and Exynos5420 SoCs.
 - reg : Offset and length of the register set for the module
-- interrupts : interrupt specifiers of SSS module interrupts, should contain
-       following entries:
-       - first : feed control interrupt (required for all variants),
-       - second : hash interrupt (required only for samsung,s5pv210-secss).
+- interrupts : interrupt specifiers of SSS module interrupts (one feed
+       control interrupt).

 - clocks : list of clock phandle and specifier pairs for all clocks  listed in
        clock-names property.
diff --git a/Documentation/devicetree/bindings/dma/fsl-mxs-dma.txt b/Documentation/devicetree/bindings/dma/fsl-mxs-dma.txt
index a4873e5e3e36..e30e184f50c7 100644
--- a/Documentation/devicetree/bindings/dma/fsl-mxs-dma.txt
+++ b/Documentation/devicetree/bindings/dma/fsl-mxs-dma.txt
@@ -38,7 +38,7 @@ dma_apbx: dma-apbx@80024000 {
              80 81 68 69
              70 71 72 73
              74 75 76 77>;
-   interrupt-names = "auart4-rx", "aurat4-tx", "spdif-tx", "empty",
+   interrupt-names = "auart4-rx", "auart4-tx", "spdif-tx", "empty",
              "saif0", "saif1", "i2c0", "i2c1",
              "auart0-rx", "auart0-tx", "auart1-rx", "auart1-tx",
              "auart2-rx", "auart2-tx", "auart3-rx", "auart3-tx";
diff --git a/Documentation/devicetree/bindings/i2c/i2c-designware.txt b/Documentation/devicetree/bindings/i2c/i2c-designware.txt
index 5199b0c8cf7a..fee26dc3e858 100644
--- a/Documentation/devicetree/bindings/i2c/i2c-designware.txt
+++ b/Documentation/devicetree/bindings/i2c/i2c-designware.txt
@@ -14,10 +14,10 @@ Optional properties :
  - i2c-sda-hold-time-ns : should contain the SDA hold time in nanoseconds.
    This option is only supported in hardware blocks version 1.11a or newer.

- - i2c-scl-falling-time : should contain the SCL falling time in nanoseconds.
+ - i2c-scl-falling-time-ns : should contain the SCL falling time in nanoseconds.
    This value which is by default 300ns is used to compute the tLOW period.

- - i2c-sda-falling-time : should contain the SDA falling time in nanoseconds.
+ - i2c-sda-falling-time-ns : should contain the SDA falling time in nanoseconds.
    This value which is by default 300ns is used to compute the tHIGH period.

关键是找到以diff开头的行,即可定位到修改过的文件名信息。

相应的awk用法如下:

awk '$1 == "diff" { print $4}'

注:上面语句的意思就是读取每一行过程中,取第一字段为”diff”的,并打印出第四个字段内容。

b/Documentation/ABI/testing/configfs-usb-gadget-loopback
b/Documentation/ABI/testing/configfs-usb-gadget-sourcesink
b/Documentation/ABI/testing/ima_policy
b/Documentation/ABI/testing/sysfs-ata
b/Documentation/HOWTO
b/Documentation/devicetree/bindings/ata/ahci-platform.txt
b/Documentation/devicetree/bindings/clock/keystone-pll.txt
b/Documentation/devicetree/bindings/crypto/samsung-sss.txt
b/Documentation/devicetree/bindings/dma/fsl-mxs-dma.txt
b/Documentation/devicetree/bindings/i2c/i2c-designware.txt
b/Documentation/devicetree/bindings/net/ethernet.txt
b/Documentation/devicetree/bindings/net/marvell-armada-370-neta.txt
b/Documentation/devicetree/bindings/pinctrl/marvell,armada-370-pinctrl.txt
b/Documentation/devicetree/bindings/pinctrl/marvell,armada-375-pinctrl.txt
b/Documentation/devicetree/bindings/pinctrl/marvell,armada-38x-pinctrl.txt
b/Documentation/devicetree/bindings/pinctrl/marvell,armada-xp-pinctrl.txt
b/Documentation/devicetree/bindings/spi/spi_pl022.txt
b/Documentation/filesystems/efivarfs.txt
b/Documentation/i2c/busses/i2c-i801
........
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值