Linux三剑客之awk命令(转,有删减)

原文链接:https://www.cnblogs.com/ginvip/p/6352157.html

awk简介

AWK 程序设计语言 ,正式定义为“样式扫描和处理语言”。它允许创建简短的程序,读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有其他的功能。

awk 适合文本处理和报表生成,是三剑客的老大。

使用方法


1

awk ‘{pattern + action}‘ {filenames}

其中 pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列命令。花括号({})不需要在程序中始终出现,但它们用于根据特定的模式对一系列指令进行分组。 pattern就是要表示的正则表达式,用斜杠括起来。

awk语言的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作。完整的awk脚本通常用来格式化文本文件中的信息。

通常,awk是以文件的一行为处理单位的。awk每接收文件的一行,然后执行相应的命令,来处理文本。

awk 的原理

再次说明, awk 对输入文件中的每一行都执行这个脚本。


1

2

3

4

awk -F":" ‘{ print $1 }‘ /etc/passwd

awk -F":" ‘{ print $1 $3 }‘ /etc/passwd

awk -F":" ‘{ print $1 " " $3 }‘ /etc/passwd

awk -F":" ‘{ print "username: " $1 "\t\tuid:" $3" }‘ /etc/passwd

下面通过几实例来了解下awk的工作原理:

实例二:已知test.txt文件内容为:


1

2

[[email protected] scripts]# cat test.txt

I am Poe,my qq is 33794712

请从该文件中过滤出‘Poe‘字符串与33794712,最后输出的结果为:Poe 33794712


1

2

[[email protected] scripts]# awk -F ‘[ ,]+‘ ‘{print $3" "$7}‘ test.txt

Poe 33794712

BEGIN 和 END 模块

实例一:统计/etc/passwd的账户人数


1

2

3

4

[[email protected] scripts]# awk ‘{count++;print $0;} END{print "user count is ",count}‘ passwd

root:x:0:0:root:/root:/bin/bash

..............................................

user count is  27

count是自定义变量。之前的action{}里都是只有一个print,其实print只是一个语句,而action{}可以有多个语句,以;号隔开。这里没有初始化count,虽然默认是0,但是妥当的做法还是初始化为0:


1

2

3

4

5

[[email protected] scripts]# awk ‘BEGIN {count=0;print "[start] user count is ",count} {count=count+1;print $0} END{print "[end] user count is ",count}‘ passwd

[start] user count is  0

root:x:0:0:root:/root:/bin/bash

...................................................................

[end] user count is  27

实例二:统计某个文件夹下的文件占用的字节数


1

2

[[email protected] scripts]# ll |awk ‘BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ",size}‘

[end]size is  1489

如果以M为单位显示:


1

2

[[email protected] scripts]# ll |awk ‘BEGIN{size=0;} {size=size+$5;} END{print "[end]size is ",size/1024/1024,"M"}‘   

[end]size is  0.00142002 M

awk运算符

awk正则运算符:


1

2

[[email protected] scripts]# awk ‘BEGIN{a="100testaa";if(a~/100/) {print "ok"}}‘

ok


1

2

[[email protected] scripts]# echo|awk ‘BEGIN{a="100testaaa"}a~/test/{print "ok"}‘

ok

awk 算术运算符:

说明,所有用作算术运算符进行操作,操作数自动转为数值,所有非数值都变为0。


1

2

3

4

[[email protected] scripts]# awk ‘BEGIN{a="b";print a++,++a}‘

0 2

[[email protected] scripts]# awk ‘BEGIN{a="20b4";print a++,++a}‘

20 22

三目运算符 ?:


1

2

3

4

[[email protected] scripts]# awk ‘BEGIN{a="b";print a=="b"?"ok":"err"}‘

ok

[[email protected] scripts]# awk ‘BEGIN{a="b";print a=="c"?"ok":"err"}‘

err

常用 awk 内置变量

字段分隔符 FS

FS="\t" 一个或多个 Tab 分隔


1

2

3

4

[[email protected] scripts]# cat tab.txt

ww   CC        IDD

[[email protected] scripts]# awk ‘BEGIN{FS="\t+"}{print $1,$2,$3}‘ tab.txt

ww   CC        IDD

FS="[[:space:]+]" 一个或多个空白空格,默认的


1

2

3

4

5

6

[[email protected] scripts]# cat space.txt

we are    studing awk now!

[[email protected] scripts]# awk -F [[:space:]+] ‘{print $1,$2,$3,$4,$5}‘ space.txt

we are  

[root[email protected] scripts]# awk -F [[:space:]+] ‘{print $1,$2}‘ space.txt

we are

FS="[" ":]+" 以一个或多个空格或:分隔


1

2

3

4

[[email protected] scripts]# cat hello.txt

root:x:0:0:root:/root:/bin/bash

[[email protected] scripts]# awk -F [" ":]+ ‘{print $1,$2,$3}‘ hello.txt

root x 0

字段数量 NF


1

2

3

4

5

[[email protected] scripts]# cat hello.txt

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin:888

[[email protected] scripts]# awk -F ":" ‘NF==8{print $0}‘ hello.txt

bin:x:1:1:bin:/bin:/sbin/nologin:888

记录数量 NR


1

2

[[email protected] scripts]# ifconfig eth0|awk -F [" ":]+ ‘NR==2{print $4}‘  ## NR==2也就是取第2行

192.168.17.129

RS 记录分隔符变量
将 FS 设置成"\n"告诉 awk 每个字段都占据一行。通过将 RS 设置成"",还会告诉 awk每个地址记录都由空白行分隔。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[[email protected] scripts]# cat recode.txt

Jimmy the Weasel

100 Pleasant Drive

San Francisco,CA 123456

Big Tony

200 Incognito Ave.

Suburbia,WA 64890

[[email protected] scripts]# cat awk.txt

#!/bin/awk

BEGIN {

        FS="\n"

        RS=""

}

{

        print $1","$2","$3

}

[[email protected] scripts]# awk -f awk.txt recode.txt

Jimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456

Big Tony,200 Incognito Ave.,Suburbia,WA 64890

OFS 输出字段分隔符


1

2

3

4

5

6

7

8

9

[[email protected] scripts]# cat hello.txt

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin:888

[[email protected] scripts]# awk ‘BEGIN{FS=":"}{print $1","$2","$3}‘ hello.txt

root,x,0

bin,x,1

[[email protected] scripts]# awk ‘BEGIN{FS=":";OFS="#"}{print $1,$2,$3}‘ hello.txt

root#x#0

bin#x#1

ORS 输出记录分隔符


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

[[email protected] scripts]# cat recode.txt

Jimmy the Weasel

100 Pleasant Drive

San Francisco,CA 123456

Big Tony

200 Incognito Ave.

Suburbia,WA 64890

[[email protected] scripts]# cat awk.txt

#!/bin/awk

BEGIN {

        FS="\n"

        RS=""

        ORS="\n\n"

}

{

        print $1","$2","$3

}

[[email protected] scripts]# awk -f awk.txt recode.txt

Jimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456

Big Tony,200 Incognito Ave.,Suburbia,WA 64890

awk 正则

正则应用

规则表达式

awk ‘/REG/{action} ‘ file,/REG/为正则表达式,可以将$0 中,满足条件的记录送入到:action 进行处理


1

2

3

4

5

6

7

8

9

[[email protected] scripts]# awk ‘/root/{print $0}‘ passwd ##匹配所有包含root的行

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

[[email protected] scripts]# awk -F: ‘$5~/root/{print $0}‘ passwd  ## 以分号作为分隔符,匹配第5个字段是root的行

root:x:0:0:root:/root:/bin/bash

[[email protected] scripts]# ifconfig eth0|awk ‘BEGIN{FS="[[:space:]:]+"} NR==2{print $4}‘

192.168.17.129

布尔表达式
awk ‘布尔表达式{action}‘ file 仅当对前面的布尔表达式求值为真时, awk 才执行代码块。


1

2

3

4

[[email protected] scripts]# awk -F: ‘$1=="root"{print $0}‘ passwd

root:x:0:0:root:/root:/bin/bash

[[email protected] scripts]# awk -F: ‘($1=="root")&&($5=="root") {print $0}‘ passwd

root:x:0:0:root:/root:/bin/bash

awk 的 if、循环和数组

条件语句

awk 提供了非常好的类似于 C 语言的 if 语句。


1

2

3

4

5

6

7

8

9

10

11

12

13

{

        if ($1=="foo"){

                if($2=="foo"){

                        print "uno"

                }else{

                        print "one"

                }

        }elseif($1=="bar"){

                print "two"

        }else{

                print "three"

        }

}

使用 if 语句还可以将代码:


1

/matchme/ { print $1 $3 $4 }

转换成:


1

2

3

4

5

{

  if ( $0 !~ /matchme/ ) {

    print $1 $3 $4

  }

}

循环结构

我们已经看到了 awk 的 while 循环结构,它等同于相应的 C 语言 while 循环。 awk 还有"do...while"循环,它在代码块结尾处对条件求值,而不像标准 while 循环那样在开始处求值。

它类似于其它语言中的"repeat...until"循环。以下是一个示例:
do...while 示例


1

2

3

4

5

{

    count=1do {

        print "I get printed at least once no matter what"

    while ( count !=1 )

}

与一般的 while 循环不同,由于在代码块之后对条件求值, "do...while"循环永远都至少执行一次。换句话说,当第一次遇到普通 while 循环时,如果条件为假,将永远不执行该循环。

for 循环

awk 允许创建 for 循环,它就象 while 循环,也等同于 C 语言的 for 循环:


1

2

3

for ( initial assignment; comparison; increment ) {

    code block

}

以下是一个简短示例:


1

2

3

for ( x=1;x<=4;x++ ) {

    print "iteration", x

}

此段代码将打印:


1

2

3

4

iteration1

iteration2

iteration3

iteration4

break 和 continue

此外,如同 C 语言一样, awk 提供了 break 和 continue 语句。使用这些语句可以更好地控制 awk 的循环结构。以下是迫切需要 break 语句的代码片断:


1

2

3

4

5

while 死循环

while (1) {

print "forever and ever..."

}

while 死循环 1 永远代表是真,这个 while 循环将永远运行下去。

以下是一个只执行十次的循环:


1

2

3

4

5

6

7

8

9

#break 语句示例

x=1

while(1) {

  print "iteration", x

  if ( x==10 ) {

    break

  }

  x++

}

这里, break 语句用于“逃出”最深层的循环。 "break"使循环立即终止,并继续执行循环代码块后面的语句。
continue 语句补充了 break,其作用如下:


1

2

3

4

5

6

7

8

9

10

11

x=1while (1) {

        if ( x==4 ) {

        x++

        continue

    }

    print "iteration", x

    if ( x>20 ) {

        break

    }

    x++

}

这段代码打印"iteration1"到"iteration21", "iteration4"除外。如果迭代等于 4,则增加 x并调用 continue 语句,该语句立即使 awk 开始执行下一个循环迭代,而不执行代码块的其余部分。如同 break 一样,

continue 语句适合各种 awk 迭代循环。在 for 循环主体中使用时, continue 将使循环控制变量自动增加。以下是一个等价循环:


1

2

3

4

5

6

for ( x=1;x<=21;x++ ) {

    if ( x==4 ) {

        continue

    }

    print "iteration", x

}

在while 循环中时,在调用 continue 之前没有必要增加 x,因为 for 循环会自动增加 x。

数组

AWK 中的数组都是关联数组,数字索引也会转变为字符串索引


1

2

3

4

5

6

7

8

9

10

11

{

    cities[1]=”beijing”

    cities[2]=”shanghai”

    cities[“three”]=”guangzhou”

    for( c in cities) {

        print cities[c]

    }

    print cities[1]

    print cities[“1”]

    print cities[“three”]

}

for…in 输出,因为数组是关联数组,默认是无序的。所以通过 for…in 得到是无序的数组。如果需要得到有序数组,需要通过下标获得。

数组的典型应用

用 awk 中查看服务器连接状态并汇总


1

2

3

netstat -an|awk ‘/^tcp/{++s[$NF]}END{for(a in s)print a,s[a]}‘

ESTABLISHED 1

LISTEN 20

统计 web 日志访问流量,要求输出访问次数,请求页面或图片,每个请求的总大小,总访问流量的大小汇总


1

2

3

4

5

6

7

8

9

awk ‘{a[$7]+=$10;++b[$7];total+=$10}END{for(x in a)print b[x],x,a[x]|"sort -rn -k1";print

"total size is :"total}‘ /app/log/access_log

total size is :172230

21 /icons/poweredby.png 83076

14 / 70546

/icons/apache_pb.gif 18608

a[$7]+=$10 表示以第 7 列为下标的数组( $10 列为$7 列的大小),把他们大小累加得到

$7 每次访问的大小,后面的 for 循环有个取巧的地方, a 和 b 数组的下标相同,所以一

条 for 语句足矣

常用字符串函数

字符串函数的应用

替换


1

2

3

awk ‘BEGIN{info="this is a test2010test!";gsub(/[0-9]+/,"!",info);print info}‘ this is a test!test!

在 info 中查找满足正则表达式, /[0-9]+/ 用”!”替换,并且替换后的值,赋值给 info 未

给 info 值,默认是$0

查找


1

2

awk ‘BEGIN{info="this is a test2010test!";print index(info,"test")?"ok":"no found";}‘

ok #未找到,返回 0

匹配查找


1

2

awk ‘BEGIN{info="this is a test2010test!";print match(info,/[0-9]+/)?"ok":"no found";}‘

ok #如果查找到数字则匹配成功返回 ok,否则失败,返回未找到

截取


1

2

awk ‘BEGIN{info="this is a test2010test!";print substr(info,4,10);}‘

s is a tes #从第 4 个 字符开始,截取 10 个长度字符串

分割


1

2

3

4

awk ‘BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}‘ 4

test 1 this 2 is 3 a

#分割 info,动态创建数组 tA,awk for …in 循环,是一个无序的循环。 并不是从数组下标

1…n 开始

原文地址:https://www.cnblogs.com/createtable/p/11101194.html

时间: 2024-08-15 05:09:37

Linux三剑客之awk命令(转,有删减)的相关文章

Linux三剑客值awk命令详解

一.awk介绍 AWK是一种优良的文本处理工具.它不仅是 Linux 中也是任何环境中现有的功能最强大的数据处理引擎之一.这种编程及数据操作语言(其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母)的最大功能取决于一个人所拥有的知识.AWK 提供了极其强大的功能:可以进行样式装入.流控制.数学运算符.进程控制语句甚至于内置的变量和函数.它具备了一个完整的语言所应具有的几乎所有精美特性.实际上 AWK 的确拥有自己的

Linux三剑客之awk命令

awk简介 awk其名称得自于它的创始人 Alfred Aho .Peter Weinberger 和 Brian Kernighan 姓氏的首个字母.实际上 AWK 的确拥有自己的语言: AWK 程序设计语言 , 三位创建者已将它正式定义为"样式扫描和处理语言".它允许您创建简短的程序,这些程序读取输入文件.为数据排序.处理数据.对输入执行计算以及生成报表,还有无数其他的功能. awk 是一种很棒的语言,它适合文本处理和报表生成,其语法较为常见,借鉴了某些语言的一些精华,如 C 语言

不看绝对后悔的Linux三剑客之awk实战精讲

一.Linux三剑客之awk命令精讲 第1章 awk基础入门 1.1 awk简介 awk不仅仅时linux系统中的一个命令,而且是一种编程语言,可以用来处理数据和生成报告(excel).处理的数据可以是一个或多个文件,可以是来自标准输入,也可以通过管道获取标准输入,awk可以在命令行上直接编辑命令进行操作,也可以编写成awk程序来进行更为复杂的运用.本章主要讲解awk命令的运用 1.2 awk环境简介 [[email protected] ~]# cat /etc/redhat-release 

linux基础学习-18-linux三剑客之awk命令精讲

快捷跳转目录: * 第1章:awk基础入门       * 1.1:awk简介 * 1.2:学完awk你可以掌握: * 1.3:awk环境简介 * 1.4:awk的格式 * 1.5:模式动作 * 1.6:awk的执行过程 * 1.6.1:小结awk执行过程 * 1.7:记录和字段 * 1.7.1:记录(行) * 1.7.2:记录分隔符-RS * 1.7.3:对$0的认识 * 1.7.4:企业面试题 * 1.7.5:awk记录知识小结 * 1.7.6:字段(列) * 1.7.7:ORS与OFS简介

Linux Shell学习--awk命令详解

(1).awk介绍 awk是由Alfred Aho .Peter Weinberger 和 Brian Kernighan于1977年开发的变成语言,awk是上述三位创建者姓的首字母. Awk的基本语法与c语言类似,如果对c语言很熟悉,那么学习awk编程也将事半功倍. Awk功能与sed相似,都是用来进行文本处理的,awk语言可以从文件或字符串中基于指定规则浏览和抽取信息,在抽取信息的基础上,才能进行其他文本操作.   awk是一款设计用于数据流的工具.它颇有玩头的原因就在于可以对列和行进行操作

Linux学习之awk命令

一. AWK 说明    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入.一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具.它在命令行中使用,但更多是作为脚本来使用. awk的处理文本和数据的方式:它逐行扫描文件,从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作.如果没有指定处理动作,则把匹配的行显示到标准输出(屏幕),如果没有指定模式,则所有被操作所

Linux Shell编程 awk命令

概述 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入(stdin).一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具.它在命令行中使用,但更多是作为脚本来使用.awk有很多内建的功能,比如数组.函数等,这是它和C语言的相同之处,灵活性是awk最大的优势. 命令的基本格式如下: [[email protected] ~]# awk '条件1 {执行语句 1} 条件 2 {执行语

【Linux基础】awk命令

1.awk命令说明 (1)awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 (2)awk处理过程: 依次对每一行进行处理,然后输出 (3)awk命令形式: awk [-F|-f|-v] 'BEGIN{} //{command1; command2} END{}' file [-F|-f|-v]   大参数,-F指定分隔符,-f调用脚本,-v定义变量 var=value '  '          引用代码块 BEGIN   初始

Linux三剑客及常用命令

要记住的符号: >2>> 2>&1 6.1 把 /data 移动到 /root目录下面#move mv [[email protected] ~]# mv /data/ /root/[[email protected] ~]# ls -l /datals: cannot access /data: No such file or directory[[email protected] ~]# ls -l /root/total 44-rw-------. 1 root ro