Awk command primary and advanced articles (details)

If you don't understand well, you can look at the picture, there are codes and running results in the picture, and you can understand.

Table of contents

Awk common parameters:

The specific gameplay of awk parameters:

1. -F specifies the delimiter

 2. -v specifies variables and default values

3. $NF represents the last field

4. NR line selection

5. && with

 6, || or

 7. FS specifies the delimiter

 8. OFS is generally used together with FS or -F 

 9. The end character designated by RS generally cooperates with OFS

Priority of awk:

Detailed explanation of awk priority

Highest priority BEGIN:

 Default priority PROGRAM (operation on data source)

 ENDlowest priority

 Fuzzy matching of awk

 Control flow writing method of awk command

awk's if judgment statement

awk for loop 

 while loop of awk

 The do...while writing method of awk

 Loop control statement beak (jump out of the current loop)

 Final awk trick


Awk common parameters:

-F specify delimiter
-v Specify variables and default values
$NF represents the last field
NR Represents the first few lines
&& and
|| or
FS input delimiter, same as -F delimiter
OFS output field separator
RS Enter record separator
$0 show the entire line
1...N first field to Nth field

The specific gameplay of awk parameters:

1. -F specifies the delimiter

#Pay attention to the writing details awk -F ":" + space + '{print $6}' There is no space in the middle and an error is reported

tail -1 /etc/passwd | awk -F ":" '{print $6}' #指定分隔符“:”并截取第六个字段

operation result:

 2. -v  specifies variables and default values

seq 1 10 > 1.txt #生成一个1到10的文件,我想计算出文件内的总和

awk -v "a=0" '{a+=$1;print a}' 1.txt | tail -1  #给a赋值为0,让a与文件内的第一个字段相加,最后结果通过tail 打印出来 但是看的出来很麻烦

##优先级END 下面会有讲

awk -v "a=0" '{a+=$1}END{print a}' 1.txt  

operation result:

3. $NF represents the last field

awk -F ":" 'NR==10{print $NF}' /etc/passwd ##截取/etc/passwd文件下面的第五行,最后一个字段

 operation result:

4. NR line selection

awk NR==44'{print $0}' /etc/passwd #打印/etc/passwd下的第44行,$0代表整行

operation result:

5. && with

Condition 1 && Condition 2 must be satisfied to be true, true returns 1, false returns 0

awk -v "a=0" 'NR==1{print (a+=$1 > 10) && (a+=$2 >10)}' 2.txt ##如果条件为假则返回0

awk -v "a=0" 'NR==1{print (a+=$1 < 10) && (a+=$2 <10)}' 2.txt ##如果为真返回 1

operation result

 6, || or

Condition 1 || Condition 2 ##Condition is satisfied and one side is true

awk -v 'a=0' 'NR==1{print (a+=$1 >10) || (a+=$2 < 10)}' 2.txt #两个条件都不满足,返回假,也就是0

awk -v 'a=0' 'NR==2{print (a+=$1 >10) || (a+=$2 < 10)}' 2.txt #第二行,两遍条件满足一遍,所以结果为1

 ##The above code sets a variable 0 for a and then lets a add to the first one of the first line, if it is greater than 10, it is true, if it is false 

operation result

 7. FS specifies the delimiter

#没有优先级写法
awk '{FS=":"}NR==2{print $1,$2}' 3.txt
#优先级写法
awk 'BEGIN{FS=":"}NR==2{print $1,$2}' 1.txt

 operation result

 8. OFS is generally used together with FS or -F 

Writing:

awk -F ":" 'NR==2{OFS="_";print $1,$2}' 1.txt  #指定分隔符":",OFS中间的间隔换成你指定的

awk 'BEGIN{FS=":";OFS="-"}NR==2{print $1,$2,$3}' 1.txt #FS用法与上面意思相同

operation result

 9. The end character designated by RS generally cooperates with OFS

The way of writing is to write RS alone:

awk 'BEGIN{RS=""}{print $0}' 2.txt ##在文本操作的时候结尾默认是回车(\n),RS指定结尾符号

operation result:

 RS and OFS are used together to write:

Note that if it is $0, it will have no effect. It can be used alone. If it is used with OFS, it cannot be the entire line.

awk 'BEGIN{RS="";OFS="##"}{print $1,$2,$3}' 2.txt  ##RS指定结尾符,OFS指定链接符

#也可以指定特殊符号
awk 'BEGIN{RS="";OFS="\n"}{print $1,$2,$3}' 2.txt

operation result

 

Priority of awk:


    Highest: BEGIN is the highest priority, it is executed before PROGRAM is executed, no data source is required, because it does not involve any data, and does not depend on PROGRAM code block; default: PROGRAM is what to do with data
    flow, it is a mandatory code block, Also the default code block. Therefore, the data source must be added when executing; the default priority (no priority is the default) is
    the lowest: END means that after the data stream is processed, if the END code block needs to be executed, the support of PROGAM must be required, and a single one cannot be executed

        Note: If only the highest or lowest priority is used, following the data source (file) will have no effect, only the default priority PROGRAM will have an effect
 

Detailed explanation of awk priority

Highest priority BEGIN:

##写法一

awk 'BEGIN{print "zhangsan"}'   #BEGIN后面不需要加任何数据源,也可以打印内容

operation result:

##写法二(赋值写法)
awk 'BEGIN{a[1]="zhangsan";a[2]="28";print a[1],a[2]}' #将a[1],a[2]赋值,并打印

awk 'BEGIN{name="zhangsan";age="28";print name,age}'  #与上面同理

operation result:

 Default priority PROGRAM (operation on data source)

        For example, I want computer memory usage (total memory - free *100 / total memory)

awk 'NR==1{a=$2}NR==2{b=$2;print(a-b)*100/a"%"}' /proc/meminfo   #将总内存赋值给a,空闲内存赋予b ,在利用算法算出内存使用率,最后加入一个“%”看起来好看

operation result:

 ENDlowest priority

The last value processed on the data

seq 1 10  > 1.txt #生成一个1到10 的文件

awk -v "a=0" '{a+=$1}END{print a}' 1.txt #$1代表第一个字段,将第一个字段的所有数相加

operation result:

   We add up the first field, we will find a lot of values, we want the last result, we will add END to see the difference between the two

 Fuzzy matching of awk

        For example, if I want to see my user information, I remember that the user starts with gx. If I don’t open the file, we can do this

awk -F ":" '$1 ~ "gx"{print $0}' /etc/passwd  #注意“~”匹配查询,后面关键字要用引号引起来一定要双引号 
#$1代表第一个字段,可以写成别字段比如第二字段就是$2...


#匹配多个写法
awk -F ":" '$1 ~"gx"{print $0};$1 ~ "roo"{print $0}' /etc/passwd 

operation result:

 Control flow writing method of awk command

parameter:

if  judgment control statement
for loop statement
while

loop statement 

do...while         cycle
loop control statement break #Only break can be used

Writing:

awk's if judgment statement

awk '{if($1>5)print $1*$2;else print $1/$2}' 2.txt #如果$1大于5,$1乘以$2,否则$1除以$2

operation result:

awk for loop 

awk '{a=0;for(i=1;i<11;i++){a+=$i}print a}' 2.txt  ##初始化值a,然后内容有几行循环就几次,最后将字段相加,就是结果了

operation result:

 while loop of awk

awk '{a=0;b=1;while(b<10){a+=$b;b++}print a}' 2.txt  #单条语句写法

##逐行写法

awk '{
sum=0
i=1
while (i<11) {
sum+=$i
i++
}
print sum 
}' 2.txt

operation result:

 The do...while writing method of awk

awk '{a=0;b=1;do{a+=$b;b++}while (b<11)print a}' 2.txt  #do...while 就是将写在while中的语句拿出来写,最后while循环

##多行写法
awk '{
a=0
b=1
do{
a+=$b
b++}
while (b<11)
print a}' 2.txt

operation result:

 Loop control statement beak (jump out of the current loop)

#单行写法
awk '{a=0;b=1;while(b<4){a+=$b;if(a>150)break;i++}print a}' 2.txt ##判断如果文件当中第一个值大于150的话,就不与后面的值相加,直接打印

#多行写法
 awk '{
a=0
b=1
while (b<11) {
a+=$b
if (a >150)
break
b++
}
print a }' 2.txt

operation result:

Do not add when the first field is greater than 150

If the first field in the file is greater than 150, it will not be added to other fields

 Final awk trick

awk 'END{print NR}' /etc/passwd  #可以打印一个文件下有多少行

awk 'END{print $0}' /etc/passwd #可以打印最后一行

awk -F ":" 'END{print NF}' /etc/passwd #可以打印文件内最后一行有多少个字段
##如果文件内有分隔符,需要指定分隔符

operation result:

Guess you like

Origin blog.csdn.net/weixin_58279299/article/details/124344404