Linux command --awk

table of Contents

 

1. Introduction

2. Main parameters

Three, the meaning of characters in awk

Four, formatted output


1. Introduction

Awk is a powerful text analysis tool. Compared with grep search and sed editing, awk is particularly powerful when it analyzes data and generates reports. Simply put, awk reads the file line by line. By default, each line is sliced ​​with a space as a separator, and the cut part is subjected to various analysis and processing. Awk is a line processor. Compared with the advantages of screen processing, there will be no memory overflow or slow processing problems when processing large files. It is usually used to format text information.

2. Main parameters

awk parameter 'BEGIN{} // {action1;action2} END{}' filename

-F   指定分隔符
-f    调用脚本
-v   定义变量
Begin{}    初始化代码块,在对每一行进行处理之前,初始化代码,主要是引用全局变量,设置FS分隔符
//            匹配代码块,可以是字符串或正则表达式
{}            命令代码块,包含一条或多条命令,多条命令用 ;  隔开
END{}      结尾代码块,在对每一行进行处理之后再执行的代码块,主要是进行最终计算或输出结尾摘要信息

Three, the meaning of characters in awk

  • $0           表示整个当前行
    $1           每行第一个字段
    NF          字段数量变量
    NR          每行的记录号,多文件记录递增
    FNR        与NR类似,不过多文件记录不递增,每个文件都从1开始
    \t            制表符
    \n           换行符
    FS          BEGIN时定义分隔符
    RS         输入的记录分隔符, 默认为换行符(即文本是按一行一行输入)
    ~            包含
    !~           不包含
    ==         等于,必须全部相等,精确比较
    !=           不等于,精确比较
    &&         逻辑与
    ||             逻辑或
    +            匹配时表示1个或1个以上
    /[0-9][0-9]+/     两个或两个以上数字
    /[0-9][0-9]*/     一个或一个以上数字
    OFS         输出字段分隔符, 默认也是空格,可以改为其他的
    ORS        输出的记录分隔符,默认为换行符,即处理结果也是一行一行输出到屏幕
    -F  [:#/]    定义了三个分隔符

    Four, formatted output

awk  '{printf  "%-5s %.2d",$1,$2}'  test

printf 表示格式输出
%格式化输出分隔符
-5表示长度为5个字符
s表示字符串类型,d表示小数

 

Guess you like

Origin blog.csdn.net/xiao__jia__jia/article/details/113788890