AWK lecture && practice

awk

Examples of display

All content displayed rows $ 0

awk '{print $0}' /etc/passwd    

Display hello world (the amount of echo control front row 1 output regardless of content)

echo asdf |awk '{print "hello world"}'  

Multi-line display hello world (there are a few lines on the display a few lines hello world file controls the number of lines in the output file)

awk '{print "hello world !"}' /etc/passwd 

Using delimiters

awk 'BEGIN{FS=":"}{print "$7"}' /etc/passwd

Using delimiters (function as)

awk -F":" '{print $7}' /etc/passwd 

Use string and tab

awk -F":" '{print "username: "$1",\t UID:"$3}' /etc/passwd

BEGIN and END block Block

通常,对于每个输入行,awk 都会执行每个脚本代码块一次。然而,在许多编程情况中,
可能需要在 awk 开始处理输入文件中的文本之前执行初始化代码。对于这种情况,awk 允许
您定义一个 BEGIN 块。我们在前一个示例中使用了 BEGIN 块。因为 awk 在开始处理输入文
件之前会执行 BEGIN 块,因此它是初始化 FS(字段分隔符)变量、打印页眉或初始化其它
在程序中以后会引用的全局变量的极佳位置。
awk 还提供了另一个特殊块,叫作 END 块。awk 在处理了输入文件中的所有行之后执行
这个块。通常,END 块用于执行最终计算或打印应该出现在输出流结尾的摘要信息。

Operators

Examples of operators && BENGIN_END

Arithmetic operators

free -m|awk 'NR==2{printf "%5.2f%\n",$3*100/$2}'

UTOOLS1573718415653.png

(a+=5) == (a=a+5)

awk 'BEGIN{a=5}{print a+=5}' #10

Logical Operators

awk 'BEGIN{a=1;b=2;print (a<b&&a==1)}'

Regular operator

awk 'BEGIN{a="100testaaa";if(a~/100/){print "has 100"}}'

Arithmetic operators

Otherwise, all as arithmetic operators operate automatically converted operand value, all non-0 values ​​are changed

awk 'BEGIN{a="B";printa++,++a}' #0 2
awk 'BEGIN{a="11abc";print a+=1}' #12

Ternary operator

awk 'BEGIN{a="B";print a=="B"?"a is B":"a not B"}' #a is B

Common awk built-in variable

variable name Attributes
$0 The current record (current row)
$1-$n ID field (separator is a separator made of field)
FS Input field delimiter in BEGIN blocks generally may be used awk -F "" specify the delimiter
RS Input record delimiter, the default is the transport
NF The number of fields in the current record is the number of columns
NO The number of records have been read out from the line number is 1 start
FNR For two files, displaying two files, line number two separate files
OFS Default output field separator is a space
ORS Output record separator, default newline
-v For example, in reference to internal variables awk '' outside the specified OFS

Examples of built-in variables

Find oldboy running program and ends

ps -ef|awk '$1=="oldboy"{print $2}'|xargs kill -9

Specify the delimiter FS

awk 'BEGIN{FS=":"}{print $1,$2,$3}'  /etc/passwd    #指定分隔符为 : ,然后读取列
awk 'BEGIN{FS="[[:space:]+]"}{print $1,$2,$3}' /etc/passwd  #分隔符为一个或多个 空格

Record number NR

ifconfig eth0|awk 'NR==2{print $2}' #IP地址

NF number of fields

awk 'BEGIN{FS=":"}{print NF}' /etc/passwd #查看一个字段有几列 (之前用FS 制定了字段分隔符)
awk -F ":" 'NF==8{print $0}' /etc/passwd #当一个字段有8列的时候输出整行

Record separator RS

awk 'BEGIN{RS=":"}{print}' /etc/passwd #把所有:替换成回车

Specifies the field separator OFS

awk 'BEGIN {FS=":";OFS="#"}{print $1,$2,$3}' /etc/passwd
#输出
    root#x#0
    bin#x#1
    daemon#x#2
    adm#x#3
    lp#x#4
    sync#x#5
    shutdown#x#6

Specify the record separator ORS

awk 'BEGIN{ORS="\t"}{print}' /etc/passwd    #把默认的回车替换成了制表符

External references internal variables

awk -F -vOFS=":" ‘{print $7,$2,$3,$4,$5,$6,$1}’ /etc/passwd

awk regular expressions

Regular application

Regular expressions

awk '/REG/{action}' filename #REG是正则表达式 可以将$0中 满足条件的记录送到:action中处理

Regular expressions example

Search a field in the record

awk '/root/{print }' /etc/passwd    #搜索*root* 在记录中

Output fifth record includes root Fields

awk -F: '$5~/root/{print }' /etc/passwd

Boolean expression

awk 'bool{action}' filename // bool?action:pass

awk 'BEGIN{FS=":"}$1=="root"{print }' /etc/passwd   #如果 第一个字段是 root 则输出

Comparison of expression

awk '$3>30' /etc/passwd

Composite Usage

awk -F: '$1=="root"||$4==0{print}' /etc/passwd

if && && loop array

if

awk -F: '{if{$3==0}{print $1}}' /etc/passwd

Very similar to the C language provides awk if statement

Awk detected using each type of user has several

UTOOLS1573721903500.png

Batch delete users using awk

UTOOLS1573722681804.png

UTOOLS1573722695685.png

Create a file different dates

UTOOLS1573723270509.png

UTOOLS1573723336028.png

cycle

awk while loop while loop structure equivalent to the C language. awk do while also a structure

while loop

while   #想要执行代码就先要满足while条件
do while #无论满不满足 , 第一次的 do 都是可以执行的


continue    #跳过此次循环 执行下次循环
break       #跳出所有循环 直接结束循环

example

for loop

The for loop takes three parameters
for (initial conditions [perform a]; [number of execution cycles] determination condition; [number of execution cycles] statements executed after successful determination)

Execution sequence: 1,2,3, {}, 2, 3, {}, 2,3} {2,3, ....} {[2 until a condition is not satisfied, out of the loop performing the following {} Code]

example

(A total of 099 cycles, 100 cycles -)

Array array

The awk array is an associative array, a numeric index will be converted to string index

{
     cities[1] ="beijing"
     cities[2] ="shanghai"
     cities["three"] ="guangzhou" 
     for(city in cities){
         print city
     }
 #exit
 }

for..in output, because the numbers are related is a group, the default is disorderly, it is a group made orderly by the subscript

TCP 11 states have taken several

UTOOLS1573724118335.png

Guess you like

Origin www.cnblogs.com/blog5434/p/11861757.html
awk