linux_ Three Musketeers _awk


[Concepts]: awk is a way to process data, generate reports formatting language, is very powerful. awk that each line of the file is recorded with a recording newline delimiter is,
      each column is a field separator field and the field is a default tab or more spaces or tabs.
      awk works by reading taking data, each row of data as one record (record) to each record field separator into several fields and each field of the output values.

        gawk - pattern scanning and processing language
            pattern scanning and processing language
[role] Description: specializes in the columns of the log file operation specializes in analyzing and processing
[Executive] Principle:


【Grammatical structures】:

  Parameter awk 'pattern matching operation command} {' information file
  sed parameters 'match condition operation instruction' information file

Basic Operation section:

  Query filter

    -F "" delimiter specified column (parameter)

      awk default symbol in space as separator column, the default plurality of consecutive spaces seen as a whole
      awk -F specified using a delimiter, said plurality of rows or a plurality of spaces between the information

-f file 指定读取程序的文件名
-v var=value 定义awk程序中使用的变量和默认值

注意:awk 程序脚本由左大括号和右大括号定义。脚本命令必须放置在两个大括号之间。由于awk命令行假定脚本是单文本字符串,所以必须将脚本包
括在单引号内。

Built-in variables []

NR == specified display line number information (Mode)

Lines in the file information represents NR
NF column information indicates file $ NF extracted last one $ (NF-1) remove the files penultimate column
FS represents the specified delimiter information -F == FS

      

Field extraction: extracting a text and a data printout

Built-in variables related field

$ 0 represents the entire line of text

$ 1 represents the first line of text in a data field

$ 2 represents the second line of text in the data field

$ N represents the N lines of text data fields

$ NF represents the last line of text in the data field

 

Exclude specific information

Analog replace modify information

print gsub display information of the specified column (act)

  modify replace awk command: `substitute 'gsub --- gawk
  awk command to replace the user: gsub (Information / to replace /" replace what information ", $ the first several columns of information do replacement)

awk程序运行优先级是:
    1)BEGIN: 在开始处理数据流之前执行,可选项
    2)program: 如何处理数据流,必选项
    3)END: 处理完数据流后执行,可选项


Test environment preparation:

Basic exercises Title:
01. xiaoyu displayed first and last ID number
one: The line numbers specified content displayed
1) find out information xiaoyu line

2) displayed information of the first column of the specified column third row


Method Two: The character information specify the content display
1) to find a second row with a column of information xiaoyu

2) show a first specified row third column column information

02. surname is Zhang, who displayed his second donation amount and the name of his
first journey: to find out the last name is zhang

second course: display output and record the name of the second donation


03. to show all people the full name and ID number ID number 41 at the beginning of
the first course: to find out the ID number information 41 beginning

the second course: showing the specified column


04. Do not show all 41 at the beginning of the ID number full name and ID, the other person's full name and ID number display

05. display all last digit ID number is 1 or 5 person's full name id number display

06. Display Xiaoyu contributions, each time there begins with $, such as $ 110 $ 220 $ --- 330. Alternatively
the first course: The specified amount is displayed

Second course: the amount of representation modified as indicated by $

练习:
01. 如何取出IP地址
第一个历程: 如何显示出IP地址
第二个历程: 显示有IP地址指定行

第三个历程: 显示IP地址



02. 如何取出文件权限数值

专业的命令获取文件的权限

高级操作部分: 对日志文件进行分析处理

awk程序的优先级

关于awk程序的执行优先级,BEGIN是优先级最高的代码块,是在执行PROGRAM之前执行的,不需要提供数据源,因为不涉及到任何数据的处理,也不依赖与PROGRAM代码块;

              PROGRAM是对数据流干什么,是必选代码块,也是默认代码块。所以在执行时必须提供数据源;

              END是处理完数据流后的操作,如果需要执行END代码块,就必须需要PROGRAM的支持,单个无法执行。

awk模式作用:
  · 普通模式: 正则信息进行匹配 比较方式匹配信息 范围模式
   · 特殊模式:
BEGIN: 在处理文件内容之前,完成BEGIN操作过程

用法

awk定义数组

数组定义方式: 数组名[索引]=值

awk运算

  1. 赋值运算 =
  2. 比较运算 > >= == < <= !=
  3. 数学运算 + - * / % ** ++ --
  4. 逻辑运算 && ||
  5. 匹配运算 ~ !~

1.赋值运算:主要是对变量或者数组赋值,如:

变量赋值 name='baism' school='ayitula'

数组赋值 array[0]=100

 

2.比较运算,如果比较的是字符串则按ascii编码顺序表比较。如果结果返回为真则用1表示,如果返回为假则用0表示

3.数学运算

4.逻辑运算

与运算:真真为真,真假为假,假假为假

或运算:真真为真, 真假为真,假假为假

5.匹配运算

Guess you like

Origin www.cnblogs.com/zhanghongqi/p/11622292.html