软件测试|Linux三剑客之awk命令详解

发布时间 2023-10-11 18:35:51作者: 霍格沃兹测试开发学社

在这里插入图片描述

简介

awk 是一种强大的文本处理工具,在 Unix 和类 Unix 系统中广泛使用。它允许您在文本文件中进行复杂的数据处理和格式化输出。awk 的名字是根据它的三位创始人AhoWeinbergerKernighan姓氏的首字母命名的。本文将详细介绍 awk 命令的基本用法和一些常见的用例。

awk 基本语法

awk 命令的基本语法如下:

awk 'pattern { action }' input_file
  • pattern:用于指定需要匹配的条件,可以是文本字符串或正则表达式。
  • { action }:在匹配到指定 pattern 的行上执行的动作。
  • input_file:要处理的输入文件名。

常见用法

  1. 打印整个文件:
awk '{ print }' input_file

上述命令将打印 input_file 文件的所有行。

  1. 打印特定列:
awk '{ print $n }' input_file

此命令将打印 input_file 文件的第n列。

  1. 打印符合条件的行:
awk '/pattern/ { print }' input_file

该命令将打印 input_file 文件中包含 "pattern" 的所有行。

  1. 使用字段分隔符:
awk -F',' '{ print $1 }' input_file

此命令将使用逗号作为字段分隔符,并打印 input_file 文件的第一个字段。

内置变量

awk 提供了一些内置变量,方便您在处理文本时使用。以下是一些常用的内置变量:

  • $0:当前行的内容。
  • $1、$2、$3...`:当前行的第一个、第二个、第三个字段等。
  • NR:当前行的行号。
  • NF:当前行的字段数。

示例用法

  1. 计算文件中数字的总和:
awk '{ sum +=$1 } END { print sum}' number.txt 

####
30

上述命令将计算 numbers.txt 文件中第一列所有数字的总和,并打印结果。

  1. 查找最长的行:
awk 'length > max_length { max_length = length; longest_line = $0 } END { print longest_line }' text.txt

######
my favorite food is jiaozi

此命令将在 text.txt 文件中查找最长的行,并打印该行。

  1. 使用自定义分隔符:
awk -F':' '{ print $1 }' /etc/passwd

##########################
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy

该命令将使用冒号作为字段分隔符,并打印 /etc/passwd 文件的第一个字段(用户名)。

awk文件中读取脚本

awk 允许将脚本命令存储到文件中,然后再在命令行中引用,比如:

cat awk.sh
{print $1 "'s home directory is " $6}
$ awk -F: -f awk.sh  /etc/passwd
root's home directory is /root
daemon's home directory is /usr/sbin
bin's home directory is /bin
sys's home directory is /dev
sync's home directory is /bin
games's home directory is /usr/games
man's home directory is /var/cache/man
lp's home directory is /var/spool/lpd
mail's home directory is /var/mail
news's home directory is /var/spool/news
uucp's home directory is /var/spool/uucp
proxy's home directory is /bin
www-data's home directory is /var/www
backup's home directory is /var/backups
list's home directory is /var/list
irc's home directory is /var/run/ircd
gnats's home directory is /var/lib/gnats
nobody's home directory is /nonexistent
systemd-network's home directory is /run/systemd
systemd-resolve's home directory is /run/systemd
systemd-timesync's home directory is /run/systemd
messagebus's home directory is /nonexistent
syslog's home directory is /home/syslog
_apt's home directory is /nonexistent
muller's home directory is /home/muller

awk.sh 脚本文件会使用 print 命令打印 /etc/passwd 文件的主目录数据字段(字段变量 $6),以及 userid 数据字段(字段变量 $1)。注意,在程序文件中,也可以指定多条命令,只要一条命令放一行即可,之间不需要用分号。

awk BEGIN关键字

awk 中还可以指定脚本命令的运行时机。默认情况下,awk 会从输入中读取一行文本,然后针对该行的数据执行程序脚本,但有时可能需要在处理数据前运行一些脚本命令,这就需要使用 BEGIN 关键字。

BEGIN 会强制 awk 在读取数据前执行该关键字后指定的脚本命令,例如:

awk 'BEGIN {print "The fruits.txt Contents:"}
> {print $0}' fruits.txt
The fruits.txt Contents:
apple
APPLE
banana
BANANA
ORANGE
orange
grape
Grape
good
Good
apple
orange
orange
orange

可以看到,这里的脚本命令中分为 2 部分,BEGIN 部分的脚本指令会在 awk 命令处理数据前运行,而真正用来处理数据的是第二段脚本命令。

awk END关键字

和 BEGIN 关键字相对应,END 关键字允许我们指定一些脚本命令,awk 会在读完数据后执行它们,例如:

awk 'BEGIN {print "player contents:"}
> {print $0}
> END {print "end of file"}' player.txt
player contents:
Muller is a German football player.
Messi is a Argentina football player.
Mbappé is a French football player.

总结

awk 命令是一种强大的文本处理工具,可用于在文本文件中进行复杂的数据处理和格式化输出。通过使用模式匹配、动作和内置变量,可以高效地处理和分析大量文本数据。在本文中,我们介绍了 awk 命令的基本用法和一些常见的用例,希望这能帮助大家更好地利用 awk 命令进行文本处理。