AWK使用小结

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

简介

这里写图片描述

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
........
The AWK Programming Language 中文版 The AWK Programming Language (AWK 程序设计语言) 中文翻译, LaTeX 排版。 译者:wuzhouhui 立即下载

相关推荐

The AWK Programming Language

awk编程 非常不错的一本书 掌握awk 丰富人生

AWK程序设计语言》笔记(1)—— AWK入门与简单案例

原文为 《The AWK Programming Language》,GitHub上有中译版,不过有些内容翻译的比较奇怪,建议跟原版对照着看https://github.com/wuzhouhui/awk 本篇的小案例基本均基于文件emp.data,三个字段分别为:员工名、每小时工资、工作时长,每一行代表一个雇员的记录 Beth 4.00 0 Dan 3.75 0 Kathy 4.00 10 Mark 5.00 20 Mary 5.50 22 Susie 4.25 18 一、 AWK入门 1...

Hehuyi_In的博客 2333

The_AWK_Programming_Language中文

AWK是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一。这种编程及数据操作语言(其名称得自于它的创始人阿尔佛雷德·艾侯、彼得·温伯格和布莱恩·柯林汉姓氏的首个字母)的最大功能取决于一个人所拥有的知识。

The AWK Programming Language》前言

前言 计算机用户花费大量的时间进行简单的,机械的数据操作--改变数据格式,核对它的有效性,找寻有特性的项目,求和,打印报告等等。所有这些工作都应该机械化。但是,每当有这样的任务,就要用C或者Pascal语言写一个特殊任务的程序真是一件令人讨厌的事。 AWK是一种编程语言,它使得利用很短的,常常是一两行的程序来完成这些任务成为可能。一段AWK程序就是一系列的模式和操作,它说明在输入数据中寻找什么...

Chinamming的专栏 287

awk 使用小结

http://sunting.blog.51cto.com/1244382/281472   awk 小结 awk 的功能非常强大,它是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入、一个或多个文件,或其它命令的输出。它支 持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本...

SAN_YUN的专栏 151

awk 使用方法小结

操作文件netstat.txt格式如下: [search@CJ-0140 ~]$ head netstat.txt  Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address               Foreign Address             State       tcp

xiwanggl的专栏 1011

AWK命令使用 小结

awk 最基本功能是在文件或字符串中基于指定规则浏览和抽取信息,它用于一行中分成数个“字段”来处理。适合处理 小型数据。  范例:      cat /etc/passwd | awk '{FS=":"}$3 {print $1 "\t" $3}'  解释:文件/etc/passwd是以":"分隔的,查看 第三栏 小于10的数据,并且只显示 帐号 与 第三栏; 执行方式

快乐&&平凡 1万+

awk使用小结

awk使用小结2017/11/9awk博大精深,本文仅持续更新在工作中频繁使用的方法。 【AWK使用逻辑判断ifelse 在逻辑处理过程中调用外部指令,并使用getline得到输出,最终赋值给变量 #!/bin/bash # #2017/11/9 #decodeetcdv3outputk/vwithbase64 ###"key":"foo...

weixin_33812433的博客 148

The AWK Programming Language 1988年 pdf

Originally developed by Alfred Aho, Brian Kernighan, and Peter Weinberger in 1977, AWK is a pattern-matching language for writing short programs to perform common data-manipulation tasks. In 1985, a new version of the language was developed, incorporating additional features such as multiple input files, dynamic regular expressions, and user-defined functions. This new version is available for both Unix and MS-DOS. This is the first book on AWK. It begins with a tutorial that shows how easy AWK is to use. The tutorial is followed by a comprehensive manual for the new version of AWK. Subsequent chapters illustrate the language by a range of useful applications, such as: * Retrieving, transforming, reducing, and validating data * Managing small, personal databases * Text processing * Little languages * Experimenting with algorithms The examples illustrates the book's three themes: showing how to use AWK well, demonstrating AWK's versatility, and explaining how common computing operations are done. In addition, the book contains two appendixes: summary of the language, and answers to selected exercises.

awk及sed的使用小结

介绍了awk,sed的使用小结

wenwuge_topsec的专栏 1260

grep,awk,sed使用小结

使用grep在文件中搜索文本 (1)在stdin中搜索匹配特定模式的文本行: [root@m01 ~]# echo -e "jfjfjfjfjaaa" | grep aa jfjfjfjfjaaa [root@m01 ~]# (2)在文件中搜索匹配特定模式的文本行: ```python [root@m01 ~]# grep ftype filestat.sh ftype=`file -b "$line" | cut -d, -f1` let statarray["$ftype"]++; for fty

qq_27546717的博客 847
上一篇: 通过KGDB进行双机内核调试
下一篇: 在Linux上使用迅雷解决方案xware
Linux先生
博客等级 码龄20年 97粉丝 43原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值