How to use linux awk command
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.
#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 参数 条件动作 文件
##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
Description | |
---|---|
After specifying the separator, the field where the current nth column is located | |
Complete one-line record | |
Field separator, the default is space | |
After the fields are separated, how many fields are there currently? | |
Current record number, row number |
参数 | 说明 |
---|---|
-F | 指定分隔字段符 |
-v | 定义或修改一个awk内部变量 |
-f | 从脚本文件中读取awk命令 |
上文谈到,awk默认的字段分隔符为空格,但是像下面这样的文本,以 # 为分隔符,就需要用到自定义分隔符;
1、显示第一列和第二列内容
awk -F "#" '{print $1,$2}' zcy2.txt
2、显示文件第一列,倒是第一列,和倒数第二列的内容
awk '{print $1,$(NF-1),$(NF-2)}' alx.txt
3、取出本机的IP地址
使用awk的方式获取的话,如果以空格为分隔符,我们发现目标字段在第二行的第二列,使用下面的命令即可,看起来,比起sed和grep命令似乎更简单;
ifconfig eth0 | awk 'NR==2{print $2}'
4、取出密码文件中的第一列和最后一列
考察对自定义输入分隔符的使用,可以看到,下面的文本文件中,可以考虑使用 : 进行分割;
awk -F ':' '{print $1,$NF}' pwd2.txt
三、OFS输出分隔符
通过上文的学习,我们知道awk命令执行后,默认采用空格分割字段,而这个空格就是默认的输出分割符,
单在某些情况下,为了将数据展示的效果更加醒目一些,就可以使用OFS的自定义输出分隔符;
仍然以上面的密码文本为例,输出第一列和最后一列的字段;
awk -F ':' -v OFS=' *** ' '{print $1,$NF}' pwd2.txt
该表默认输出分隔符,直接在awk后面使用: -v OFS=‘自定义输出分隔符’
四、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
ARGV使用
先来看下面这条命令的执行结果
awk 'NR==1,NR==3{print ARGV[0],ARGV[1],$0}' alx.txt
可以发现,在输出的每一行记录前面,拼上了 awk 和 alx.txt这两个字段,这两个字段就是这行命令整体解析出来的2个内置参数;
自定义变量
看下面这条命令输出效果,通过-v参数,可以自定义变量进行参数传递;
awk -v myname="zcy" 'BEGIN{print "我的名字是?" ,myname}'
五、awk格式化输出
在上文,我们接触的是awk的输出功能,主要使用了 print 这个进行输出,它只能对文本进行简单的输出,但是并不能美化或者修改输出格式;
printf 格式化输出
如果对C语言有过了解的同学,对printf 并不陌生,使用这个命令(函数)可以对文本进行格式化输出;
printf与print的几点区别
- printf 需要指定format;
- format 用于指定后面的每个 item输出格式;
- printf 语句不会自动打印换行符; \n ; print 默认添加换行符;
如下,假如我们直接使用 printf 这样操作,看下效果
awk '{printf $0}' alx.txt
明显来说,把所有内容都输出到同一行了,这时候,就需要使用 printf的格式化输出来控制;
awk '{printf "%s\n", $0}' alx.txt
再看一个案例,使用 printf 将文本中的每一列添加前置输出
awk '{printf "第一列:%s 第二列:%s 第三列:%s\n" ,$1,$2,$3}' alx.txt
六、awk模式pattern
上文了解到,awk的语法如下 :
awk [option] ‘pattern[action]’ file …
而且我们了解到,awk是按行处理文本,以上都是关于 print 相关,接下来,聊聊pattern相关的内容;
在pattern中,有个比较常见的pattern,BEGIN和END;
- BEGIN 模式是处理文本之前需要执行的动作;
- END模式是处理完成所有的行之后执行的操作;
awk 'BEGIN{print "小明在学linux"}'
或者下面这样
awk 'BEGIN{print "小明在学linux"} {print $0}END{print "处理结束"}' alx.txt
注意:BEGIN 和 END分别放到处理文本内容前后即可
awk如果不指定模式是按行处理,如果指定了模式,只有符合模式的才会被处理
awk常用模式
关系运算符 | 说明 |
---|---|
< | 小于 |
<= | 小于等于 |
== | 等于 |
!= | 不等于 |
>= | 大于等于 |
~ | 匹配正则 |
!~ | 不匹配正则 |
1、打印前三行的文本内容
awk 'NR<=3{print $0}' alx.txt
2、匹配密码文本中含有 zcy 的行
awk '/^zcy/{print $0}' pwd.txt
3、格式化输出 /etc/passwd 的部分字段
awk -F ":" 'BEGIN{print"用户名\t\t\t字段1\t\t 字段2\t\t 权限"} {printf "user:%-20s%-20s%-20s%-20s\n", $1,$4,$5,$7}' pwd.txt
4、找出pwd文件中nologin的用户
awk '/\/sbin\/nologin$/{print NR,$0}' pwd.txt
5、找出 下面这个区间的文本行
awk '/^daemon/,/^operator/{print NR,$0}' pwd.txt
相关推荐:《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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
