AWK, the Three Musketeers of Linux

Source: Internet
Author: User

#AWK命令基础显示

Print the second area of the Install.log file that contains the data field row

awk ‘/data/ {print $2}’ install.log

View the first line of Num10.txt

head -n 1 num10.txt结果:10cat !$  #cat上次打开的文件,cat num10.txt

AWK displays only the first line:

NR number of rows in row

awk reads a file one line at a read;

awk ‘NR==1‘ num10.txt结果:10

awk prints the last line:

awk ‘END{print $0}‘ num10.txt结果: 1 最后一行

Show the last column:

NF Number of fields column

awk -F ‘ ‘ ‘{print $NF}‘ qqq结果:lll222

Do not display the first row

awk ‘NR != 1‘ num10.txt

Do not display the last column

awk -F ‘ ‘ ‘BEGIN{OFS="---"}{print $1,$2,$3}‘ qqq  结果:123---123---c1fsda---ddd3---222

ORS, the default is the line break after each line ends

awk -F " " ‘BEGIN{OFS="";ORS=""}{for(i = 1;i < NF; i++){print $i" "}{printf "\n"}}‘ qqq   
Base operations

Suddenly think of a python solution, to two instances ~

There is now a file QQQ, which reads as follows:

1 2 3) 4 5

2 3 4) 5 6

3 4 5) 6 7

实例1:(求每行的和)with open("qqq") as f:    j = 0    for line in f:  # line 是字符串数据类型。        li = line.strip().split()  # split生成列表数据        sum = 0        for i in li:            sum += int(i)            # print(line,sum)        j += 1        print(j,sum)结果:1 152 203 25实例2:(求所有值的和)with open("qqq") as f:    sum = 0    for line in f:  # line 是字符串数据类型。        li = line.strip().split()  #split生成列表数据        for i in li:            sum += int(i)    print(sum)结果:60
Character matching

There is now a file grade, which reads as follows:

john 10 3  78 94 88andrea 20 90 75 90 86jasper 90 450 90 92 84sun 60 50 80 98 87

Print rows for the first column as Sun

awk  ‘{if($1 == "sun")print $0}‘ grade结果:sun 60 50 80 98 87

Prints rows with the first column longer than 4 and the second column greater than 40.

awk ‘{if (length($1) > 4 && $2 > 40) print $0}‘ gradejasper 90 450 90 92 84awk 不指定-F,就是默认空格或者table来分割。

Regular match: ~

awk  ‘{if($1 ~ "su")print $0}‘ grade结果:sun 60 50 80 98 8711. 匹配noarch字段,如果有,则显示整行$awk ‘$2 ~ /noarch/’ install.log12. 匹配不存在noarch字段的行,如果有,则显示整行$awk ‘$2 !~ /noarch/’ install.log
Process Control Two single quotes can have multiple {},if statements, such as the following

Example 1:/etc/passwd change root to admin, and the other is the list of common user.

awk -F: ‘{if ($1 == "root")    printf "%-15s: %s\n", $1,"Admin"else    print "%-15s: %s\n", $1,"Common User"}‘ /etc/passwd  

Example 2: How many ordinary users, UID > 1000

awk -F: -v sum=0 ‘{    # sum=0  这里不能有空格if ($3 > 1000)  sum++}END{  print sum  }‘  /etc/passwd结果:47

The process is as follows,

If process

If‘{if(条件){action}}‘

For process

for‘{for (条件) {action}}‘

Exercise: Print all rows/values that last column is less than or equal to 6 (or use Python ....) )

with open("qqq") as f:    for line in f:        print(line.strip(),type(line.strip()))  # line in f 中 line 是字符串,strip()去掉换行符。        li = line.strip().split()        a = int(li[-1])        if a > 6:            pass            # print(line)            # print(a)        else:            pass结果:1 2 3 4 5 <class ‘str‘>2 3 4 5 6 <class ‘str‘>3 4 5 6 7 <class ‘str‘>awk ‘{if ($NF <= 6) print $0}‘ qqq  #AWK一条命令解决
--------------------------------------awk Command format

The awk instruction consists of a combination of patterns, actions, or patterns and actions.

‘pattern {action}‘pattern 即模式,也可以理解为条件,你要找谁,高矮,胖瘦,等,都是条件。action:即动作,可以理解为干啥,找到人之后你要干嘛
awk Execution Process
a) awk屈辱第一行内容b) 判断是否符合模式中得条件,匹配则执行action,不匹配,继续读取下一行c) 继续读取下一行d) 直到读取到最后一行(EOF end of file)
Fields and records

Field fields, regions, fields $1,$2, etc., represent the first field, the second field, and so on.

Record records, which can be understood as a whole line

内置变量:RS == record seperator 即行得分隔符NR == number of record 行号ORS == output record seperator 输出时候得分隔符
Awk has a regular match.
^cool 匹配所有以cool开头得字符串cool$ 匹配所有以cool结尾得字符串a+b 匹配一个或多个a 加上b得行ifconfig eth0 |awk -F "[ :]+" ‘NR==2 {print $3 }‘ifconfig eth0 | awk -F "netmask|broadcast" ‘NR==2{print $3}‘怎么匹配1,不匹配到11:awk --posix -F: ‘$3~/1{2,}/{print $0}‘ test.awk    出现两次得可以出来,写成{1,} 也还是都出来了awk -F: ‘$3~/\<1\>/{print $0}‘ test.awk  \< 或 \b:词首锚定;用于单词模式的左侧;\> 或 \b:词尾锚定;用于单词模式的右侧;\<PATTERN\>:匹配整个单词;netstat -lantp | awk -F "[:/ ]+" ‘{print $5,"\t",$10}‘| sort 查询服务得端口
Begin and end modules detailed
找空行:grep -c ‘^$‘ test.awkawk -v a=0 ‘/^$/{a=a+1}END{print a}‘ test.awkawk -v a=0 -F: ‘$3>=5{a=a+1;(用;号分割)print a(a++)}END{print a}‘ test.awk

AWK, the Three Musketeers of Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.