awk 命令补充说明:
AWK
是另一种流行的流编辑器,类似于
SED
。 awk 的基本功能是在文件中搜索包含一种或多种模式的行或其他文本单元。 当一行与其中一个模式匹配时,会对该行执行特殊操作。
有几种方法可以运行 awk。 如果程序很短,最简单的方法是在命令行上运行它:
awk PROGRAM inputfile(s)
如果必须对多个文件进行多次更改,可能定期对多个文件进行更改,则将 awk 命令放在脚本中会更容易。 这是这样读的:
awk -f PROGRAM-FILE inputfile(s)
awk 中最常用的程序是
print
,我们很快就会看到。awk 中的打印命令从输入文件中输出选定的数据。变量
$1
,
$2
,
$3
, ...,
$N
保存输入行的第一个、第二个、第三个直到最后一个字段的值。 变量
$0
(零)保存整行的值。
如上图:要打印大小
$2
和使用%
$5
,请使用
df -h | awk '{print $2,$5}'
,
注意
:
$2,$5
:它们之间的
,
将用
空格
分隔输出。
$ df -h | awk '{print $2,$5}'
-------------------
Size Use%
40G 25%
7.8G 0%
7.8G 0%
7.8G 1%
7.8G 0%
1000G 12%
1.6G 0%
1.6G 0%
awk 命令语法:
awk [-F sepstring] [-v assignment]... program [argument...]
awk [-F sepstring] -f progfile [-f progfile]... [-v assignment]...[argument...]
awk 命令选项:
-F sepstring
Define the input field separator. This option shall be
equivalent to:
-v FS=sepstring
except that if -F sepstring and -v FS=sepstring are
both used, it is unspecified whether the FS assignment
resulting from -F sepstring is processed in command
line order or is processed after the last -v
FS=sepstring. See the description of the FS built-in
variable, and how it is used, in the EXTENDED
DESCRIPTION section.
-f progfile
Specify the pathname of the file progfile containing an
awk program. A pathname of '-' shall denote the
standard input. If multiple instances of this option
are specified, the concatenation of the files specified
as progfile in the order specified shall be the awk
program. The awk program can alternatively be specified
in the command line as a single argument.
-v assignment
The application shall ensure that the assignment
argument is in the same form as an assignment operand.
The specified variable assignment shall occur prior to
executing the awk program, including the actions
associated with BEGIN patterns (if any). Multiple
occurrences of this option can be specified.
awk 命令实例:
awk 在空格分隔的文件中打印第五列(又名字段):
awk '{print $5}' filename
awk 在空格分隔的文件中打印包含“某物”的行的第二列:
awk '/something/ {print $2}' filename
awk 打印文件中每一行的最后一列,使用逗号(而不是空格)作为字段分隔符:
awk -F ',' '{print $NF}' filename
awk 对文件第一列中的值求和并打印总数:
awk '{s+=$1} END {print s}' filename
awk 对第一列中的值求和并漂亮地打印这些值,然后是总数:
awk '{s+=$1; print $1} END {print "--------"; print s}' filename
awk 从第一行开始每三行打印一次:
awk 'NR%3==1' filename
awk
Formatting
格式化字段输出:
$ df -h | sort -rnk 5 | head -3 | awk '{ print "Partition " $6 "\t: " $5 " full!" }'
Partition /mnt/sdb6 : 99% full!
Partition /mnt/sdb5 : 97% full!
Partition /home : 89% full!
awk / gawk 字符串格式化:
awk 打印命令和正则表达式,例如,从
/etc
目录列出以字母
a
或
c
开头并以
.conf
结尾的文件并打印第
9
个字段:
$ ls -l /etc/ | awk '/\
awk 脚本方式,我们使用
BEGIN
、
END
,正则表达式配置到
test.awk
中,然后运行:
BEGIN { print "*** WARNING WARNING WARNING ***" }
awk 的
RS
、
ORS
与
FS
、
OFS
及
NR
、
NF
:
RS:Record Separator,记录分隔符
ORS:Output Record Separate,输出当前记录分隔符
FS:Field Separator,字段分隔符
OFS:Out of Field Separator,输出字段分隔符
NR:Number of Records 当前行数
NF:Number of Fields 字段数量
假设 test.txt 文件有 100 行,awk 只取第 20 到第 30 行内容:
awk '{if(NR>=20 && NR<=30) print $1}' test.txt
awk 扩展阅读:
Linux awk command
Uso del comando AWK en Linux y UNIX con ejemplos - 通过示例在 Linux 和 UNIX 上使用 AWK 命令
Linux 删除乱码怪异字符的目录,rm 怪异文件名
sed 命令详解
vim 命令详解
nvim 命令详解
sort 命令详解
egrep 命令
pgrep 命令
bzgrep 命令
grep 命令
awk 命令
sed 命令
cut 命令