linux中awk命令

AWK is suitable for pattern search and processing. The script runs to search one or more files to identify matching patterns and if the said patterns perform specific tasks. In this guide, we take a look into AWK Linux command and see what it can do.

AWK适用于模式搜索和处理。 该脚本运行以搜索一个或多个文件以识别匹配的模式,以及所述模式是否执行特定任务。 在本指南中,我们研究了AWK Linux命令并了解它可以做什么。

AWK可以执行哪些操作? (What Operations can AWK do?)

  • Scanning files line by line逐行扫描文件
  • Splitting each input line into fields将每条输入线分成多个字段
  • Comparing input lines and fields to patterns比较输入行和字段与模式
  • Performing specified actions on matching lines在匹配行上执行指定的操作

AWK命令的实用性 (AWK Command Usefulness)

  • Changing data files更改数据文件
  • Producing formatted reports生成格式化的报告

awk命令的编程概念 (Programming Concepts for awk command)

  • Format output lines格式化输出行
  • Conditional and loops条件循环
  • Arithmetic and string operations算术和字符串运算

AWK语法 (AWK Syntax)

$ awk options 'selection _criteria {action }' input-file > output-file

To demonstrate more about AWK usage, we are going to use the text file called file.txt

为了演示有关AWK用法的更多信息,我们将使用名为file.txt的文本文件。

1st column => Item,
2nd column => Model
3rd column => Country
4th column => Cost

第一栏=>项目,
第二列=>型号
第三栏=>国家
第四列=>费用

Awk命令示例 (Awk Command Examples)

打印特定列 (Printing specific columns)

To print the 2nd and 3rd columns, execute the command below.

要打印第二列和第三列,请执行以下命令。

$ awk '{print $2 "\t" $3}' file.txt

Output

输出量

打印文件中的所有行 (Printing all lines in a file)

If you wish to list all the lines and columns in a file, execute

如果要列出文件中的所有行和列,请执行

$ awk ' {print $0}' file.txt

Output

输出量

打印与特定图案匹配的所有行 (Printing all lines that match a specific pattern)

if you want to print lines that match a certain pattern, the syntax is as shown

如果要打印与特定模式匹配的行,则语法如下所示

$ awk '/variable_to_be_matched/ {print $0}' file.txt

For instance, to match all entries with the letter ‘o’, the syntax will be

例如,要匹配所有带有字母“ o”的条目,语法为

$ awk '/o/ {print $0}' file.txt

Output

输出量

To match all entries with the letter ‘e’

使所有条目与字母“ e”匹配

$ awk '/e/ {print $0}' file.txt

Output

输出量

打印与特定图案匹配的列 (Printing columns that match a specific pattern)

When AWK locates a pattern match, the command will execute the whole record. You can change the default by issuing an instruction to display only certain fields.

当AWK找到一个模式匹配项时,该命令将执行整个记录。 您可以通过发出仅显示某些字段的指令来更改默认值。

For example:

例如:

$ awk '/a/ {print $3 "\t" $4}' file.txt

The above command prints the 3rd and 4th columns where the letter ‘a’ appears in either of the columns

上面的命令将打印第三和第四列,其中字母“ a”出现在任一列中

Output

输出量

计数和打印匹配图案 (Counting and Printing Matched Pattern)

You can use AWK to count and print the number of lines for every pattern match. For example, the command below counts the number of instances a matching pattern appears

您可以使用AWK来计数和打印每个模式匹配的行数。 例如,以下命令计算匹配模式出现的实例数

$ awk '/a/{++cnt} END {print "Count = ", cnt}' file.txt

Output

输出量

打印行数多于或少于字符数 (Print Lines with More or less than a No. of Characters)

AWK has a built-in length function that returns the length of the string. From the command $0 variable stores the entire line and in the absence of a body block, the default action is taken, i.e., the print action. Therefore, in our text file, if a line has more than 18 characters, then the comparison results true, and the line is printed as shown below.

AWK具有内置的长度函数,该函数返回字符串的长度。 从命令$ 0变量存储整行,并且在没有主体块的情况下,将采取默认操作,即打印操作。 因此,在我们的文本文件中,如果一行中的字符超过18个,则比较结果为true,并且该行将如下所示打印。

$ awk 'length($0) > 20' file.txt

Output

输出量

将AWK的输出保存到其他文件 (Saving output of AWK to a different file)

If you wish to save the output of your results, use the > redirection operator. For example

如果希望保存结果输出,请使用>重定向运算符。 例如

$ awk '/a/ {print $3 "\t" $4}' file.txt > Output.txt

You can verify the results using the cat command as shown below

您可以使用cat命令验证结果,如下所示

$ cat output.txt

Output

输出量

结论 (Conclusion)

AWK is another simple programming script that you can use to manipulate text in documents or perform specific functions. The shared commands are a few or the many you are yet to know or come across.

AWK是另一个简单的编程脚本,可用于处理文档中的文本或执行特定功能。 共享命令是您尚不知道或遇到的几个命令。

翻译自: https://www.journaldev.com/24871/awk-command-linux-unix

linux中awk命令

linux中awk命令_Linux / Unix中的AWK命令相关推荐

  1. unix和linux命令_Linux / UNIX中的cp命令

    unix和linux命令 In this guide, we focus on cp command in Linux/Unix systems. cp command – short for cop ...

  2. unix grep命令_Linux / UNIX中的Grep命令

    unix grep命令 In Linux and Unix Systems Grep, short for "global regular expression print", i ...

  3. linux ftp下载文件_Linux系统中10个使用Wget命令下载文件示例

    wget 是一个从网络上自动下载文件的命令行工具,支持通过 HTTP.HTTPS.FTP 三个最常见的 TCP/IP协议 下载,并可以使用 HTTP 代理.它是一个非交互式工具,非常适合通过脚本或者在 ...

  4. linux中使用u盘和光驱的命令_Linux文件操作高频使用命令

    0.新建操作: mkdir abc #新建一个文件夹touch abc.sh #新建一个文件 1.查看操作 查看目录: ll #显示目录文件详细信息du -h 文件/目录 #查看大小pwd #显示路径 ...

  5. zip unzip命令行_Linux / Unix中的Zip和Unzip命令

    zip unzip命令行 In this guide, we will focus on zip and unzip commands in Linux. zip command is a utili ...

  6. linux mysql 僵尸进程_Linux 系统中僵尸进程

    Linux 系统中僵尸进程和现实中僵尸(虽然我也没见过)类似,虽然已经死了,但是由于没人给它们收尸,还能四处走动.僵尸进程指的是那些虽然已经终止的进程,但仍然保留一些信息,等待其父进程为其收尸. 僵尸 ...

  7. linux java 僵尸进程_Linux 系统中僵尸进程

    Linux 系统中僵尸进程和现实中僵尸(虽然我也没见过)类似,虽然已经死了,但是由于没人给它们收尸,还能四处走动.僵尸进程指的是那些虽然已经终止的进程,但仍然保留一些信息,等待其父进程为其收尸.配图源 ...

  8. linux nand 坏块_Linux内核中NAND Flash坏块管理

    由于NAND Flash的现有工艺不能保证NAND的Memory Array在其生命周期中保持性能的可靠,因此在NAND芯片出厂的时候,厂家只能保证block 0不是坏块,对于其它block,则均有可 ...

  9. linux proc 目录清理_Linux文件及目录管理命令基础

    目录 前言 下面是常用命令的介绍,这些命令都是以后会用到的,熟练掌握便是最终的目标. 第一章 Linux文件及目录管理命令基础 1.1 pwd: 显示当前所在位置的信息 [功能说明] 显示当前所在目录 ...

最新文章

  1. Python读取内容UnicodeDecodeError错误
  2. vue+springboot-前台怎么回显本地文件夹下的图片
  3. 功能性农业谋定乡村振兴路径-万祥军:灌区农业大健康产业
  4. POJ 1061 青蛙的约会(扩展欧几里得)
  5. php如何修改xml中element值,php修改xml节点的值
  6. macOS 10.11.* 安装scrapy
  7. mysql tomcat列表增删改查_Tomcat-Database
  8. HashMap的扩容机制
  9. 360公司2019秋招空中宣讲会开启,送海量面试直通卡!
  10. [COCOS2DX-LUA]0-001.利用ClippingNode实现放大镜功能
  11. deepin linux下解决Qt搜狗输入法无法输入中文
  12. java中包的概念及作用_Java中包的概念和使用实战
  13. Redis教程--基于docker搭建redis文档服务
  14. 武汉大学计算机学院csc,2018年春武汉大学CSC公派出国留学录取名单
  15. 安卓linux获取最高权限获取,安卓root是什么意思(获取手机最高权限)
  16. 你为什么总是爱拖延?这个我知道
  17. (深入篇)漫游语音识别技术—带你走进语音识别技术的世界
  18. VR视频为什么都是弯的?
  19. 给大家推荐一款超低功耗的4G低功耗摄像头方案强大的AI功能
  20. 使用PyTorch中的预训练模型进行图像分类

热门文章

  1. Java Collection Framework
  2. GOF业务场景的设计模式-----责任链模式
  3. iOS开发--添加定位功能
  4. ajax:dataType
  5. ubuntu下安装beanstalkd
  6. 正则表达式 查找以某些字符开始 某些字符结束的匹配项 解决之道
  7. Linux操作系统文件链接问题
  8. [转载] c++的vector赋值方法汇总
  9. [转载] python的__del__()方法
  10. (2)网络基础之IP