Use Linux AWK commands to make data processing more efficient!
在Linux系统中,我们经常需要对各种不同格式的数据进行处理和分析。这时候,一个简单又强大的工具就派上用场了 —— AWK。AWK是一种文本处理工具,它可以快速地处理文本文件,并且非常适合用于日志分析、数据提取、统计报表等各种任务。在本文中,我们将为您介绍AWK的基本用法和常见应用场景,让您轻松掌握这个数据处理利器。
0、基本用法
awk是一个强大的文本分析工具,简单来说awk就是把文件逐行读入,(空格,制表符)为默认分隔符将每行切片,切开的部分再进行各种分析处理
awk命令格式如下
awk [-F field-separator] 'commands' input-file(s)
[-F 分隔符]是可选的,因为awk使用空格,制表符作为缺省的字段分隔符,因此如果要浏览字段间有空格,制表符的文本,不必指定这个选项,但如果要浏览诸如/etc/passwd文件,此文件各字段以冒号作为分隔符,则必须指明-F选项
echo "this is a test" | awk '{ print $0 }' ## 输出为 this is a test
shell
读取用户输入的字符串发现|,代表有管道。|左右被理解为简单命令,即前一个(左边)简单命令的标准输出指向后一个(右边)标准命令的标准输入awk
会根据分隔符将行分成若干个字段,为整行,1为第一个字段,$2 为第2个地段,依此类推…
为打印一个字段或所有字段,使用print命令。这是一个awk
动作
echo "this is a test" | awk '{ print $1 }' ## 输出为 this echo "this is a test" | awk '{ print $1, $2 }' ## 输出为 this is
/etc/passwd
的文件内容如下
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
举几个简单的小需求
1、只显示/etc/passwd的账户
awk -F : '{ print $1 }' /etc/passwd ## 输出为 root bin daemon adm lp

2、显示/etc/passwd的第1列和第7列,用逗号分隔显示,所有行开始前添加列名start1,start7,最后一行添加,end1,end7
awk -F ':' 'BEGIN {print "start1,start7"} {print $1 "," $7} END {print "end1,end7"}' /etc/passwd ## 输出为 start1,start7 root,/bin/bash bin,/sbin/nologin daemon,/sbin/nologin adm,/sbin/nologin lp,/sbin/nologin end1,end7
BEGIN语句在所有文本处理动作执行之前被执行,END在所有文本处理动作执行之后被执行
3、统计/etc/passwd文件中,每行的行号,每行的列数,对应的完整行内容
awk -F : '{ print NR " " NF " " $0 }' /etc/passwd ## 输出为 1 7 root:x:0:0:root:/root:/bin/bash 2 7 bin:x:1:1:bin:/bin:/sbin/nologin 3 7 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 7 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 7 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
1、支持内置变量
上面示例中NR
,和NF
其实就是awk
的内置变量,一些内置变量如下
变量名 解释 FILENAMEawk浏览的文件名 FS设置输入字段分隔符,等价于命令行-F选项 NF 浏览记录的字段个数 NR 已读的记录数
2、支持函数
输出字符串的长度
awk 'BEGIN { print length("this is a text") }'
## 输出为
14
将/etc/passwd
的用户名变成大写输出
awk -F ':' '{ print toupper($1) }' /etc/passwd
## 输出为
ROOT BIN DAEMON ADM LP
常用函数如下
函数名 作用 toupper(s)返回s的大写 tolower(s) 返回s的小写 length(s) 返回s长度 substr(s,p) 返回字符串s中从p开始的后缀部分
3、支持条件操作,正则表达式匹配
显示/etc/passwd中有daemon的行
awk -F ‘:’ ‘$0 ~ /daemon/’ /etc/passwd
## 输出为
daemon:x:2:2:daemon:/sbin:/sbin/nologin awk条件操作符 操作符 描述 if while do/while for break continue
输出第一个字段的第一个字符大于d的行
awk -F ':' '{ if ($1 > "d") { print $1 } else { print "-" } }' /etc/passwd
## 输出为
root - daemon - lp
可以把流程控制语句放到一个脚本中,然后调用脚本执行,如test.sh的内容如下
{ if ($1 > "d") { print $1 } else { print "-" } }
用如下方式执行,效果一样
awk -F ':' -f test.sh /etc/passwd
## 输出为
root - daemon - lp
5、应用场景
小编用awk进行文本分析比较少,主要用来写脚本
如一个weibo-interface-1.0.jar应用,启动脚本如下
start.sh nohup java -jar weibo-interface-1.0.jar >out 2>&1 &
关闭脚本如下,kill.sh
kill -9 `jps -l | grep 'weibo-interface-1.0.jar' | awk '{print $1}'`
jps -l的输出如下
70208 com.st.kmp.main.KmpService 31036 com.st.cis.main.BaiduAnalysisService 66813 weibo-interface-1.0.jar
还有就是关闭hadoop集群的所有DataNode节点(不知道hadoop的可以认为DataNode是一个集群应用),假如一个个机器jps,查看pid,kill。很麻烦,直接写了一个脚本,依次ssh到各个节点,然后执行如下命令即可
kill `jps | grep 'DataNode' | awk '{print $1}'`
jps的输出为
508 DataNode 31481 JournalNode 31973 NodeManager
总的来说,AWK是一个非常强大的数据处理工具。通过灵活的语法和功能,我们可以快速地处理不同格式的数据,并且生成各种形式的报表和统计数据。在本文中,我们介绍了AWK的基本概念、语法和常见应用场景,并且通过实例演示了如何使用AWK处理数据。希望本文能够帮助读者更好地理解AWK,提高数据处理的效率!
The above is the detailed content of Use Linux AWK commands to make data processing more efficient!. 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

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 five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

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 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.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

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.
