How awk works

1. The working principle of awk

  • Read text line by line, separated by space or tab by default, save the separated fields to built-in variables, and execute editing commands according to modes or conditions
  • The sed command is often used to process a whole line, while awk tends to divide a line into multiple "fields" and then process it. The reading of awk information is also read line by line, and the execution result can be printed and displayed with the field data through the print function. In the process of using the awk command, you can use the logical operators "&&" to mean "and", "||' to mean "or", "!" to mean "not"; you can also perform simple mathematical operations, such as +,- , *, /, %, ^ represent addition, subtraction, multiplication, division, remainder and power respectively

2. Command format

awk 选项 '模式或条件 {操作} ' 文件1 文件2 ...
awk -f 脚本文件 文件1 文件2...

3. Common built-in variables of awk (can be used directly)

FS :列分割符。指定每行文本的字段分隔符,默认为空格或制表位。与"-F"作用相同
NF :当前处理的行的字段个数
NR :当前处理的行的行号(序数)
$0 :当前处理的行的整行内容
$n :当前处理行的第n个字段(第n列)
FILENAME :被处理的文件名
RS :行分隔符。awk从文件上读取资料时,将根据RS的定义把资料切割成许多条记录,而awk一次仅读入一条记录,以进行处理。预设值是'\n'

4. Output text by line

Insert picture description here
Insert picture description here

  • BEGIN mode means that before processing the specified text, you need to perform the specified action in the BEGIN mode; awk processes the specified text, and then executes the specified action in the END mode. The END{} statement block is often put in Print results and other statements

5. Output text by field

Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

6. Invoke Shell commands through pipes and double quotes

Insert picture description here
Insert picture description here

Insert picture description here

7、getline

  • When there is no redirection character "<" or "|" on the left and right of getline, getline acts on the current file, and reads the first line of the current file to the variable var or $0 followed by it; it should be noted that because awk has already processed the getline before A line is read, so the return result of getline is interlaced
  • When there are redirection characters "<" or "|" on the left and right of getline, getline acts on the directional input file. Since the file is just opened and has not been read into a line by awk, only getline reads it, then getline returns this The first line of the file, not every other line
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_53496478/article/details/114923227