Unix/Linux 中常用的 AWK 基础命令示例

您所在的位置:网站首页 linux第一列求和 Unix/Linux 中常用的 AWK 基础命令示例

Unix/Linux 中常用的 AWK 基础命令示例

2023-03-13 12:32| 来源: 网络整理| 查看: 265

AWK 简介

AWK 是 Unix/Linux 等类 Unix 操作系统中标准工具之一,是一个强大的文本处理工具,和 sed, grep并称文本处理三剑客。同时 AWK 也是一门专为文本处理而设计的解释性编程语言,具有自己独特的编码风格。也就是说,在某些领域内,大多数编程语言能干的事,它都能干,而且能干得更好。AWK 由 Alfred Aho、Peter Weinberger 和 Brian Kernighan 这三位计算机科学家在 1977 年所开发的,故取了这三位计算机科学家的姓氏首字母作为它的名字(AWK) 。

AWK 非常适合数据报告、分析和提取,并支持数组、关联数组、函数、变量、循环和正则表达式。当前的 Unix/Linux 系统中所使用大多都是 原始 AWK 实用程序的改进版本 Gawk、Mawk 和 Nawk 等等。它逐行搜索扫描文件,将输入的每行分割成字段,将输入行或字段与模式进行比较,并对匹配的结果执行相应的操作。正因为它的这些特性使得其在文本处理方面有着相当重要的地位。

示例演示所使用的数据

为了更好的展示出这些常用的 AWK 命令的实际效果,我们使用了 GNU AWK 官方文档中的 mail-list 和 inventory-shipped 这两个例子里面的文本内容来作为AWK命令的输入,其中 mail-list 中的信息包含有一些人的名字以及他们的电子邮件地址,电话号码和一些其他相关信息。最后一栏中的 “A” 表示这个人是熟人。最后一列的 “F” 表示这个人是朋友,而 “R” 则表示该人是亲属。文本具体内容如下:

Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A ean-Paul 555-2127 [email protected] R

而 inventory-shipped 中的数据则表示某仓库一年中发货的信息。其中的列分别包含月份、绿色箱子发货数量、红色箱子发货数量、橙色包裹发货数量和蓝色包裹发货数量。总共有 16 行数据,涵盖了去年的 12 个月和今年的前四个月的发货信息,其中使用空行来讲两年的数据分割开来。详细数据内容如下:

Jan 13 25 15 115 Feb 15 32 24 226 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208 Jun 31 42 75 492 Jul 24 34 67 436 Aug 15 34 47 316 Sep 13 55 37 277 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Jan 21 36 64 620 Feb 26 58 80 652 Mar 24 75 70 495 Apr 21 70 74 5141. 使用 -F 参数设置列分隔符user@localhosts:awk$ cat mail-list Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R user@localhosts:awk$ awk -F "@" '{print $1}' mail-list Amelia 555-5553 amelia.zodiacusque Anthony 555-3412 anthony.asserturo Becky 555-7685 becky.algebrarum Bill 555-1675 bill.drowning Broderick 555-0542 broderick.aliquotiens Camilla 555-2912 camilla.infusarum Fabius 555-1234 fabius.undevicesimus Julie 555-6699 julie.perscrutabor Martin 555-6480 martin.codicibus Samuel 555-3430 samuel.lanceolis Jean-Paul 555-2127 jeanpaul.campanorum2. 打印文件中所有的行user@localhosts:awk$ awk '{print}' mail-list Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R user@localhosts:awk$ awk '{print $0}' mail-list Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R3. 打印文件中的第 N 行user@localhosts:awk$ awk 'NR == 1 {print $0}' mail-list Amelia 555-5553 [email protected] F user@localhosts:awk$ awk 'NR == 3 || NR==4 || NR==5 {print $0}' mail-list Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R user@localhosts:awk$ awk '(NR == 3 || NR==4 || NR==5) {print $0}' mail-list Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R4. 打印文件中的除第一行外的所有行user@localhosts:awk$ awk 'NR != 1 {print $0}' mail-list Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R5. 为文件添加行号user@localhosts:awk$ awk '{print NR"\t"$0}' mail-list 1 Amelia 555-5553 [email protected] F 2 Anthony 555-3412 [email protected] A 3 Becky 555-7685 [email protected] A 4 Bill 555-1675 [email protected] A 5 Broderick 555-0542 [email protected] R 6 Camilla 555-2912 [email protected] R 7 Fabius 555-1234 [email protected] F 8 Julie 555-6699 [email protected] F 9 Martin 555-6480 [email protected] A 10 Samuel 555-3430 [email protected] A 11 Jean-Paul 555-2127 [email protected] R6. 打印邮件类型为 gmail 或者 hotmail 的行user@localhosts:awk$ awk '/gmail|hotmail/ { print $0 }' mail-list Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Martin 555-6480 [email protected] A user@localhosts:awk$ awk '/gmail/||/hotmail/ { print $0 }' mail-list Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Martin 555-6480 [email protected] A7. 打印邮件为 hotmail 且包含 A 字母的行user@localhosts:awk$ awk '/hotmail/&&/A/ { print $0 }' mail-list Anthony 555-3412 [email protected] A Bill 555-1675 [email protected] A Martin 555-6480 [email protected] A8. 为文件内容添加表头user@localhosts:awk$ awk 'BEGIN{print "Name\t\tPhoneNumber\t\tE-Mail\t\tRelationship"} {print}' mail-list Name PhoneNumber E-Mail Relationship Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R9. 为文件内容添加总结user@localhosts:awk$ awk 'BEGIN{print "Name\t\tPhoneNumber\t\tE-Mail\t\tRelationship"} {print} END {print ">>> 11 Contac ts in Total"}' mail-list Name PhoneNumber E-Mail Relationship Amelia 555-5553 [email protected] F Anthony 555-3412 [email protected] A Becky 555-7685 [email protected] A Bill 555-1675 [email protected] A Broderick 555-0542 [email protected] R Camilla 555-2912 [email protected] R Fabius 555-1234 [email protected] F Julie 555-6699 [email protected] F Martin 555-6480 [email protected] A Samuel 555-3430 [email protected] A Jean-Paul 555-2127 [email protected] R >>> 11 Contacts in Total10. 打印文件指定的列并用制表符分隔user@localhosts:awk$ awk '{print $1"\t\t"$3}' mail-list Amelia [email protected] Anthony [email protected] Becky [email protected] Bill [email protected] Broderick [email protected] Camilla [email protected] Fabius [email protected] Julie [email protected] Martin [email protected] Samuel [email protected] Jean-Paul [email protected]. 打印文件的最后一列或文件倒数第二列user@localhosts:awk$ awk '{print $NF}' mail-list F A A A R R F F A A R user@localhosts:awk$ awk '{print $(NF-1)}' mail-list [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]. 指定输出的分隔符user@localhosts:awk$ awk 'OFS="||" {print $1,$2}' mail-list Amelia||555-5553 Anthony||555-3412 Becky||555-7685 Bill||555-1675 Broderick||555-0542 Camilla||555-2912 Fabius||555-1234 Julie||555-6699 Martin||555-6480 Samuel||555-3430 Jean-Paul||555-212713. 筛选出 inventory-shipped 中第五列大于400的数据user@localhosts:awk$ awk '$5 > 400 {print}' inventory-shipped Apr 31 52 63 420 Jun 31 42 75 492 Jul 24 34 67 436 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Jan 21 36 64 620 Feb 26 58 80 652 Mar 24 75 70 495 Apr 21 70 74 51414. 筛选出第一个字段中包含大写字母“J`”的所有行user@localhosts:awk$ awk '$1 ~ /J/' inventory-shipped Jan 13 25 15 115 Jun 31 42 75 492 Jul 24 34 67 436 Jan 21 36 64 62015. 筛选出第一个字段中不包含大写字母“J”的所有行user@localhosts:awk$ awk '$1 !~ /J/' inventory-shipped Feb 15 32 24 226 Mar 15 24 34 228 Apr 31 52 63 420 May 16 34 29 208 Aug 15 34 47 316 Sep 13 55 37 277 Oct 29 54 68 525 Nov 20 87 82 577 Dec 17 35 61 401 Feb 26 58 80 652 Mar 24 75 70 495 Apr 21 70 74 51416. 使用 printf 格式化输出user@localhosts:awk$ awk '{ printf "%-15s %s\n", $1, $2 }' mail-list Amelia 555-5553 Anthony 555-3412 Becky 555-7685 Bill 555-1675 Broderick 555-0542 Camilla 555-2912 Fabius 555-1234 Julie 555-6699 Martin 555-6480 Samuel 555-3430 Jean-Paul 555-212717. 对数据按行求和并输出到行尾user@localhosts:awk$ awk 'sum = $2 + $3 + $4 + $5 {print $0"\t"sum}' inventory-shipped Jan 13 25 15 115 168 Feb 15 32 24 226 297 Mar 15 24 34 228 301 Apr 31 52 63 420 566 May 16 34 29 208 287 Jun 31 42 75 492 640 Jul 24 34 67 436 561 Aug 15 34 47 316 412 Sep 13 55 37 277 382 Oct 29 54 68 525 676 Nov 20 87 82 577 766 Dec 17 35 61 401 514 Jan 21 36 64 620 741 Feb 26 58 80 652 816 Mar 24 75 70 495 664 Apr 21 70 74 514 679 user@localhosts:awk$ awk '{a[NR]=$0; for(i=2;i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3