awk

awk是一种样式扫描与处理工具

 

1、首先先看下awk的参数

[thxy@linux-3 ~]# awk --hlep
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:          GNU long options:
        -f progfile             --file=progfile
        -F fs                   --field-separator=fs
        -v var=val              --assign=var=val
        -m[fr] val
        -W compat               --compat
        -W copyleft             --copyleft
        -W copyright            --copyright
        -W dump-variables[=file]        --dump-variables[=file]
        -W exec=file            --exec=file
        -W gen-po               --gen-po
        -W help                 --help
        -W lint[=fatal]         --lint[=fatal]
        -W lint-old             --lint-old
        -W non-decimal-data     --non-decimal-data
        -W profile[=file]       --profile[=file]
        -W posix                --posix
        -W re-interval          --re-interval
        -W source=program-text  --source=program-text
        -W traditional          --traditional
        -W usage                --usage
        -W version              --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
        gawk '{ sum += $1 }; END { print sum }' file
        gawk -F: '{ print $1 }' /etc/passwd

2、学习awk的内置变量

 

变量 功能 默认
NF 当前记录中的字段个数,代表列号  
NR 读出的记录数,代表行号,从1开始  
FS 输入字段分隔符 默认是空格 空格或者tab
RS 输入的记录他隔符默 认为换行符 换行
OFS 输出字段分隔符 默认也是空格 空格或者tab
ORS 输出的记录分隔符,默认为换行符 换行

 

3、打印第一列,并显示行号

[thxy@linux-3 ~]# awk -F : '{print NR,$1}' 1.txt
1 root
2 ROOT
3 bin
4 daemon
5 adm
6 lp

 

 

4、打印第一行放倒数第二行,显示行号

[thxy@linux-3 ~]# awk -F : '{print NR,$1,$NF-1}' 1.txt
1 root -1
2 ROOT -1
3 bin -1
4 daemon -1
5 adm -1
6 lp -1

 

 

5、显示以5的倍数行号的打印

[thxy@linux-3 ~]# awk -F : 'NR%10==5{print NR,$0}' /etc/passwd
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
15 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
25 avahi:x:70:70:Avahi daemon:/:/sbin/nologin
35 squid:x:23:23::/var/spool/squid:/sbin/nologin

 

6、显示除5倍数行号以外的打印

[thxy@linux-3 ~]# awk -F : 'NR%10!=5{print NR,$0}' /etc/passwd

 

7、显示第一行到第三行,并使用$0打印显示的行

[thxy@linux-3 ~]# awk -F : 'NR==1,NR==3{print NR,$0}' 1.txt
1 root:x:0:0:root:/root:/bin/bash
2 ROOT:x:0:0:root:/root:/bin/bash
3 bin:x:1:1:bin:/bin:/sbin/nologin

 

 

8、查看以root开头,并打印出第一列

[thxy@linux-3 ~]# awk -F : '/^root/{print NR,$1}' 1.txt
1 root

 

 

9、查找第一列2个字符的用户,打印出来

[thxy@linux-3 ~]# awk -F : '{if(length($1) == 2) print NR,$0}' 1.txt
6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

 

 

10、在以root开头,第一列和第二列加上#号

[thxy@linux-3 ~]# awk -F : '/^root/{print "#"$1"#"$2}' 1.txt
#root#x

 

11、判断第一列不等于root的就全部打印

[thxy@linux-3 ~]# awk -F : '{if($1 !~/root/) print $0}' 1.txt
ROOT:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

这种方式也可以[thxy@linux-3 ~]# awk -F : '{if($1 != "root") print $0}' 1.txt

 

12、判断第一列等于root的就打印

[thxy@linux-3 ~]# awk -F : '{if($1 ~/root/) print $0}' 1.txt
root:x:0:0:root:/root:/bin/bash

这种方式也可以[thxy@linux-3 ~]# awk -F : '{if($1 == "root") print $0}' 1.txt

 

13、判断语句(注意每个判断完后,必须要使用;号,像print $0;)

[thxy@linux-3 ~]# awk -F : '{if ( $1 == "1" )print $0;else if ( $1 == "2" )print $0;else print NR}' 1.txt  
1
2
3
4
5
6

 

14、BEGIN和END的学习

        BEGIN读前处理

        END读后处理

[thxy@linux-3 ~]# awk -F : 'ENDIN{i=1} {i++} END{print i}' 1.txt
6

 

 

15、while循环(b=b+i也可以写成b+=i)

     [thxy@linux-3 thxy]# awk 'BEGIN{a=50;b=0;while(i<=a){b=b+i;i++}print b}'
1275

 

16、for循环

[thxy@linux-3 thxy]# awk 'BEGIN{for (i=1;i<=10;i++) print i}'
1
2
3
4
5
6
7
8
9
10

17、break 跳出循环
[thxy@linux-3 ~]# awk 'BEGIN{for (i=1;i<=10;i++) {if(i==5) break} print i}'
5

 

 

 

18、数组

[thxy@linux-3 ~]# awk 'BEGIN{ary[1]="test";ary[2]="test1";ary[3]="test2";for(i in ary) print ary[i]}'
test
test1
test2

 

 

19、打印第一行

[thxy@linux-3 ~]# awk -F : 'NR==1{print $0}' 1.txt
root:x:0:0:root:/root:/bin/bash

 

 

20、打印最后一行

[thxy@linux-3 ~]# awk 'END{print}' 1.txt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

 

 

21、简单取整

[thxy@linux-3 ~]# awk 'BEGIN{print int(2.200)}'
2

 

 

22、从另一个文件中读入(不过只显示读入的文件内容)

[thxy@linux-3 ~]# awk -F: 'BEGIN{getline<"3.txt";print $0}'
daemon:x:2:2:daemon:/sbin:/sbin/nologin

 

 

23、从命令中读入

[thxy@linux-3 ~]# awk 'BEGIN {"uname -a"|getline ;print $3}'
2.6.18-164.el5

 

 

24、使用awk脚本添加用户

[thxy@linux-3 ~]# cat awk.sh
#!/bin/awk -f
{
  system("useradd "$1"; echo "$2"|passwd --stdin "$1)
}
[thxy@linux-3 ~]# cat name
aaa   1111111
bbb   1111111
ccc   1111111
[thxy@linux-3 ~]# ./awk.sh name
Changing password for user aaa.
passwd: all authentication tokens updated successfully.
Changing password for user bbb.
passwd: all authentication tokens updated successfully.
Changing password for user ccc.
passwd: all authentication tokens updated successfully.

 

 

25、生成随机数

[thxy@linux-3 ~]# cat 6.txt
^dd%^d#$HFC(jfieJFa3abb6OEAASFFEIFjfiejfdsf
[thxy@linux-3 ~]# awk -F '' 'BEGIN{srand();for(i=1;i<=8;i++)a[i]=int(rand()*100%7+1)}{for(i=1;i<=8;i++)printf $a[i];printf RS}' 6.txt
^d^^##%^
[thxy@linux-3 ~]# awk -F '' 'BEGIN{srand();for(i=1;i<=8;i++)a[i]=int(rand()*100%7+1)}{for(i=1;i<=8;i++)printf $a[i];printf RS}' 6.txt
d^^^^#d%

不过发现它没有shell好用,shell生成随机数脚本:http://youzao.blog.51cto.com/3946111/737746