How to use linux awk command

青灯夜游
Release: 2023-02-01 18:51:21
Original
10941 people have browsed it

In Linux, the awk command is a text data processing tool, suitable for formatting text files and performing more complex processing and analysis of text files. The syntax is "awk [option] 'pattern[action]' file. .." Awk has powerful text formatting capabilities. For example, for a bunch of seemingly irregular log files, text files, etc., after passing the awk command, the formatted output will be a professional style that can be used for application-level data analysis.

How to use linux awk command

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

There is a more powerful text data processing tool in the Linux system, which is awk. It was born in the late 1970s, which may be one of the reasons why it influenced so many Linux users.

Some people have speculated that the name of the awk command comes from the word awkward. In fact, this is not the case. There are three designers of this command. Their last names are Aho, Weingberger and Kernighan. awk is taken from these three, which are the initial letters of the master's last name.

awk has powerful text formatting capabilities. For example, for a bunch of seemingly irregular log files, text files, etc., after passing the awk command, the formatted output can be used as a professional application. Level data analysis style;

awk is like a programming language, supporting conditional judgment, arrays, loops and many other functions;

linux three musketeers

  • grep is good at simply searching or matching text content;

  • sed is good at text editing and processing matched text content;

  • awk, suitable for formatting text files and performing more complex processing and analysis of text files;

awk theoretical basis

1. awk syntax

awk  [option]  'pattern[action]'  file ...

awk   参数       条件动作           文件
Copy after login

How to use linux awk command

##action refers to action. awk is good at text formatting and can output formatted The result, so the most commonly used actions are print and printf

2, awk processing text content mode

    awk uses spaces as the delimiter by default , and multiple spaces are also recognized as one space as a separator;
  • awk processes files line by line. After one line is processed, the next line is processed;
  • awk can be separated according to the user specified character to work, if not specified, the default is a space;

1. awk built-in variables

Built-in variable Description$nAfter specifying the separator, the field where the current nth column is located$0Complete one-line recordFSField separator, the default is spaceNF(Number of fields)After the fields are separated, how many fields are there currently?NR(Number of records)Current record number, row number

更多的内置变量,可通过 man awk命令进行查看

简单案例展示

提前准备一个文本,内容如下

How to use linux awk command

1、输出第二列内容

awk '{print $2}' alx.txt
Copy after login

How to use linux awk command

2、输出多列内容

直接在第一步后面的基础上追加,中间用 “,” 分割

awk '{print $2,$3}' alx.txt
Copy after login

How to use linux awk command

3、查看第三行内容

考察对NR的使用,NR表示第N行记录的模式匹配

awk 'NR==3{print $0}' alx.txt
Copy after login

How to use linux awk command

输出多行

awk 'NR==5,NR==6{print $0}' alx.txt
Copy after login

How to use linux awk command

4、输出从第3到第五行,并显示行号

awk 'NR==3,NR==5 {print NR,$0}' alx.txt
Copy after login

How to use linux awk command

5、自定义输出内容

某些情况下,需要给每一列添加类似于excel的表头信息,就可以考虑使用awk的自定义输出;

awk '{print "第一列: "$1,"第二列: "$2}' alx.txt
Copy after login

How to use linux awk command

需要注意的是大括号外面的使用 ’ 单引号,括号里面的使用双引号

二、awk参数

参数说明
-F指定分隔字段符
-v定义或修改一个awk内部变量
-f从脚本文件中读取awk命令

上文谈到,awk默认的字段分隔符为空格,但是像下面这样的文本,以 # 为分隔符,就需要用到自定义分隔符;

How to use linux awk command

1、显示第一列和第二列内容

awk -F "#" '{print $1,$2}' zcy2.txt
Copy after login

How to use linux awk command

2、显示文件第一列,倒是第一列,和倒数第二列的内容

awk '{print $1,$(NF-1),$(NF-2)}' alx.txt
Copy after login

1How to use linux awk command

3、取出本机的IP地址

1How to use linux awk command

使用awk的方式获取的话,如果以空格为分隔符,我们发现目标字段在第二行的第二列,使用下面的命令即可,看起来,比起sed和grep命令似乎更简单;

ifconfig eth0 | awk 'NR==2{print $2}'
Copy after login

1How to use linux awk command

4、取出密码文件中的第一列和最后一列

考察对自定义输入分隔符的使用,可以看到,下面的文本文件中,可以考虑使用 : 进行分割;

1How to use linux awk command

awk -F ':' '{print $1,$NF}' pwd2.txt
Copy after login

1How to use linux awk command

三、OFS输出分隔符

通过上文的学习,我们知道awk命令执行后,默认采用空格分割字段,而这个空格就是默认的输出分割符,

单在某些情况下,为了将数据展示的效果更加醒目一些,就可以使用OFS的自定义输出分隔符;

仍然以上面的密码文本为例,输出第一列和最后一列的字段;

awk -F ':' -v OFS=' *** ' '{print $1,$NF}' pwd2.txt
Copy after login

该表默认输出分隔符,直接在awk后面使用: -v OFS=‘自定义输出分隔符’

1How to use linux awk command

四、awk变量

awk参数

参数说明
-F指定分隔字段符
-v定义或修改一个awk内部变量
-f从脚本文件中读取awk命令

对于awk来讲,变量分为:内置变量和自定义变量

awk内置变量

参数说明
FS输入字段分隔符,默认为空白字符
OFS输出字段分隔符,默认为空白字符
RS输入记录分隔符,指定输入时的换行符
ORS输出记录分隔符,输出时用指定符号替换换行符
NF当前行的字段个数,字段数量
NR行号,当前处理文本行的行号
FNR各文件分别计数的行号
FILENAME当前文件名
ARGC命令行参数个数
ARGV数组,保存的是命令行所给定的各个参数

比较常用的内置变量包括: NR,NF,FNR

FILENAME 使用

FILENAME 为awk的内置变量,通过下面这个命令,可以看到在每行记录之前,输出了当前文件名称;

awk 'NR==1,NR==3{print FILENAME,$0}' alx.txt
Copy after login

1How to use linux awk command

ARGV使用

先来看下面这条命令的执行结果

awk 'NR==1,NR==3{print ARGV[0],ARGV[1],$0}' alx.txt
Copy after login

1How to use linux awk command

可以发现,在输出的每一行记录前面,拼上了 awk 和 alx.txt这两个字段,这两个字段就是这行命令整体解析出来的2个内置参数;

自定义变量

看下面这条命令输出效果,通过-v参数,可以自定义变量进行参数传递;

awk -v myname="zcy" 'BEGIN{print "我的名字是?" ,myname}'
Copy after login

1How to use linux awk command

五、awk格式化输出

在上文,我们接触的是awk的输出功能,主要使用了 print 这个进行输出,它只能对文本进行简单的输出,但是并不能美化或者修改输出格式;

printf 格式化输出

如果对C语言有过了解的同学,对printf 并不陌生,使用这个命令(函数)可以对文本进行格式化输出;

printf与print的几点区别

  • printf 需要指定format;
  • format 用于指定后面的每个 item输出格式;
  • printf 语句不会自动打印换行符; \n ; print 默认添加换行符;

如下,假如我们直接使用 printf 这样操作,看下效果

awk '{printf $0}' alx.txt
Copy after login

How to use linux awk command

明显来说,把所有内容都输出到同一行了,这时候,就需要使用 printf的格式化输出来控制;

awk '{printf "%s\n", $0}' alx.txt
Copy after login

2How to use linux awk command

再看一个案例,使用 printf 将文本中的每一列添加前置输出

awk '{printf "第一列:%s   第二列:%s   第三列:%s\n" ,$1,$2,$3}' alx.txt
Copy after login

2How to use linux awk command

六、awk模式pattern

上文了解到,awk的语法如下 :

awk [option] ‘pattern[action]’ file …

而且我们了解到,awk是按行处理文本,以上都是关于 print 相关,接下来,聊聊pattern相关的内容;

在pattern中,有个比较常见的pattern,BEGIN和END;

  • BEGIN 模式是处理文本之前需要执行的动作;
  • END模式是处理完成所有的行之后执行的操作;
awk 'BEGIN{print "小明在学linux"}'
Copy after login

2How to use linux awk command

或者下面这样

awk 'BEGIN{print "小明在学linux"} {print $0}END{print "处理结束"}' alx.txt
Copy after login

2How to use linux awk command

注意:BEGIN 和 END分别放到处理文本内容前后即可

awk如果不指定模式是按行处理,如果指定了模式,只有符合模式的才会被处理

awk常用模式

关系运算符说明
<小于
<=小于等于
==等于
!=不等于
>=大于等于
~匹配正则
!~不匹配正则

1、打印前三行的文本内容

awk &#39;NR<=3{print $0}&#39; alx.txt
Copy after login

2How to use linux awk command

2、匹配密码文本中含有 zcy 的行

awk &#39;/^zcy/{print $0}&#39; pwd.txt
Copy after login

2How to use linux awk command

3、格式化输出 /etc/passwd 的部分字段

awk -F ":" &#39;BEGIN{print"用户名\t\t\t字段1\t\t     字段2\t\t     权限"} {printf "user:%-20s%-20s%-20s%-20s\n", $1,$4,$5,$7}&#39; pwd.txt
Copy after login

2How to use linux awk command

4、找出pwd文件中nologin的用户

2How to use linux awk command

awk &#39;/\/sbin\/nologin$/{print NR,$0}&#39; pwd.txt
Copy after login

2How to use linux awk command

5、找出 下面这个区间的文本行

How to use linux awk command

awk &#39;/^daemon/,/^operator/{print NR,$0}&#39; pwd.txt
Copy after login

3How to use linux awk command

相关推荐:《Linux视频教程

The above is the detailed content of How to use linux awk command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template