AWK基本命令

awk基本命令

1.awk '{print $1}' /etc/fstab
默认以空格为分割符的第一个字段打印出来,空格不算

2.awk '{print $1 "\t " $3}' /etc/fstab
打印两个字段,

lu@lu-VirtualBox:~$ awk '{print $1 "\t " $3}' /etc/fstab
#     static
#     
#     'blkid'
#     this
#     works
#     
#     system>
#     was
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf     ext4
#     was

对比
lu@lu-VirtualBox:~$ awk '{print $1 "\t " $2}' /etc/fstab
#     /etc/fstab:
#     
#     Use
#     device;
#     that
#     
#     <file
#     /
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf     /
#     swap
UUID=990ee444-dada-441f-9fff-ca2efffaeb4e     none

完整文档:
lu@lu-VirtualBox:~$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=990ee444-dada-441f-9fff-ca2efffaeb4e none            swap    sw              0       0

将字段说明加到输出中:
lu@lu-VirtualBox:~$ awk '{print "device:" $2 "\t " "fstype:" $3}' /etc/fstab
device:/etc/fstab:     fstype:static
device:     fstype:
device:Use     fstype:'blkid'
device:device;     fstype:this
device:that     fstype:works
device:     fstype:
device:<file     fstype:system>
device:/     fstype:was
device:/     fstype:ext4
device:swap     fstype:was
device:none     fstype:swap

lu@lu-VirtualBox:~$ cat /etc/passwd | head -n10
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

在临时文件中操作:
lu@lu-VirtualBox:~$ head -n10 /etc/passwd >/tmp/passwd.piece
lu@lu-VirtualBox:~$ cat /tmp/passwd.piece 
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

#-F设置分隔符,默认空白字符
lu@lu-VirtualBox:~$ awk -F ":" '{print "user: "$1 "\tSHELL: "$7}' /tmp/passwd.piece 
user: root    SHELL: /bin/bash
user: daemon    SHELL: /usr/sbin/nologin
user: bin    SHELL: /usr/sbin/nologin
user: sys    SHELL: /usr/sbin/nologin
user: sync    SHELL: /bin/sync
user: games    SHELL: /usr/sbin/nologin
user: man    SHELL: /usr/sbin/nologin
user: lp    SHELL: /usr/sbin/nologin
user: mail    SHELL: /usr/sbin/nologin
user: news    SHELL: /usr/sbin/nologin


#设置多个分隔符
lu@lu-VirtualBox:~$ awk -F "[\t ]+" '{print "device: "$1 "\tFSTYPE: "$3}' /etc/fstab
device: #    FSTYPE: static
device: #    FSTYPE: 
device: #    FSTYPE: 'blkid'
device: #    FSTYPE: this
device: #    FSTYPE: works
device: #    FSTYPE: 
device: #    FSTYPE: system>
device: #    FSTYPE: was
device: UUID=7e390bf8-16ef-419d-b02c-6fbaba4362cf    FSTYPE: ext4
device: #    FSTYPE: was
device: UUID=990ee444-dada-441f-9fff-ca2efffaeb4e    FSTYPE: swap

#将头10行输入管道再用AWK命令格式输出每行的第1个,第7个字符
lu@lu-VirtualBox:~$ head -n10 /etc/passwd | awk -F":" '{print "User:" $1 "\tSHELL:"$7}'
User:root    SHELL:/bin/bash
User:daemon    SHELL:/usr/sbin/nologin
User:bin    SHELL:/usr/sbin/nologin
User:sys    SHELL:/usr/sbin/nologin
User:sync    SHELL:/bin/sync
User:games    SHELL:/usr/sbin/nologin
User:man    SHELL:/usr/sbin/nologin
User:lp    SHELL:/usr/sbin/nologin
User:mail    SHELL:/usr/sbin/nologin
User:news    SHELL:/usr/sbin/nologin

#SHELL操作
#BEGIN,END代码块
#-f指定awk文件传递到命令中
lu@lu-VirtualBox:~$ cat fs.awk
cat: fs.awk: 没有那个文件或目录
lu@lu-VirtualBox:~$ vi fs.awk
lu@lu-VirtualBox:~$ cat fs.awk 
BEGIN{
    FS=":"
}
{
    print "user: "$1 "\tshell:"$7
}
#将冒号“:”赋值给FS变量,命令将默认以:为分隔符
lu@lu-VirtualBox:~$ head -n10 /etc/passwd| awk -f fs.awk 
user: root    shell:/bin/bash
user: daemon    shell:/usr/sbin/nologin
user: bin    shell:/usr/sbin/nologin
user: sys    shell:/usr/sbin/nologin
user: sync    shell:/bin/sync
user: games    shell:/usr/sbin/nologin
user: man    shell:/usr/sbin/nologin
user: lp    shell:/usr/sbin/nologin
user: mail    shell:/usr/sbin/nologin
user: news    shell:/usr/sbin/nologin

#END块一般用于统计数据、打印输出之类的操作
lu@lu-VirtualBox:~$ vi search.awk
BEGIN{
        print "how many people with nologin?"
}
/nologin/{++adder}#正则表达式
#匹配成功则执行{}之前的内容,adder变量计数+1
END{
        print "'nologin' apperars " adder " times"
}
lu@lu-VirtualBox:~$ vi search.awk
lu@lu-VirtualBox:~$ awk -f search.awk /etc/passwd
how many people with nologin?
'nologin' apperars 17 times

#此处有坑
lu@lu-VirtualBox:~$ awk '/^\r/{print "this is an empty line."} ' /test/First.java 
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.

lu@lu-VirtualBox:~$ awk '/^$/{print "this is an empty line."} ' /test/test.txt 
this is an empty line.
this is an empty line.
this is an empty line.

#查找有OUT的行
lu@lu-VirtualBox:~$ awk '/out/{print "this is an empty line."} ' /test/First.java 
this is an empty line.
this is an empty line.
this is an empty line.
this is an empty line.


#查找使用BASH作为登录SHELL的用户名
lu@lu-VirtualBox:~$ awk -F":" '/bash/{print $1}' /etc/passwd
root
lu
lu@lu-VirtualBox:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
lu:x:1000:1000:lu,,,:/home/lu:/bin/bash

AWK 数组
 
AWK 可以使用关联数组这种数据结构,索引可以是数字或字符串。

AWK关联数 组也不需要提前声明其大小,因为它在运行时可以自动的增大或减小。

数组使用的语法格式:

array_name[index]=value
array_name:数组的名称
index:数组索引
value:数组中元素所赋予的值
创建数组
接下来看一下如何创建数组以及如何访问数组元素:

lu@lu-VirtualBox:~$ awk 'BEGIN {sites["hello"]="world"; print sites["hello"]}'
world

lu@lu-VirtualBox:~$ awk 'BEGIN {
> sites["hello"]="world";
> sites["google"]="www.google.com";
> print sites["hello"] "\n" sites["google"]
> }'
world
www.google.com

也可以使用如下格式访问数组元素:

array_name[index] 
删除数组元素
我们可以使用 delete 语句来删除数组元素,语法格式如下:

delete array_name[index]
下面的例子中,数组中的 google 元素被删除(删除命令没有输出):

lu@lu-VirtualBox:~$ awk 'BEGIN {
sites[0]="world";
sites[1]="www.google.com"
delete sites[0];
print sites[0] "\n" sites[1]
}'

www.google.com
lu@lu-VirtualBox:~$ awk 'BEGIN {
sites[0]="world";
sites[1]="www.google.com"
#delete sites[0];
print sites[0] "\n" sites[1]
}'
world
www.google.com


lu@lu-VirtualBox:~$ awk 'BEGIN {
sites["hello"]="world";
sites["google"]="www.google.com"
delete sites["google"];
print sites["hello"] "\n" sites["google"]
}'
world

#多维数组
AWK 本身不支持多维数组,不过我们可以很容易地使用一维数组模拟实现多维数组。

如下示例为一个 3x3 的三维数组:

100 200 300
400 500 600
700 800 900
以上实例中,array[0][0] 存储 100,array[0][1] 存储 200 ,依次类推。为了在 array[0][0] 处存储 100, 我们可以使用如下语法: array["0,0"] = 100。

我们使用了 0,0 作为索引,但是这并不是两个索引值。事实上,它是一个字符串索引 0,0。

下面是模拟二维数组的例子:

lu@lu-VirtualBox:~$ awk 'BEGIN {
> array["0,0"] = 100;
> array["0,1"] = 200;
> array["0,2"] = 300;
> array["1,0"] = 400;
> array["1,1"] = 500;
> array["1,2"] = 600;
> # 输出数组元素
> print "array[0,0] = " array["0,0"];
> print "array[0,1] = " array["0,1"];
> print "array[0,2] = " array["0,2"];
> print "array[1,0] = " array["1,0"];
> print "array[1,1] = " array["1,1"];
> print "array[1,2] = " array["1,2"];
> }'
array[0,0] = 100
array[0,1] = 200
array[0,2] = 300
array[1,0] = 400
array[1,1] = 500
array[1,2] = 600

在数组上可以执行很多操作,比如,使用 asort 完成数组元素的排序,或者使用 asorti 实现数组索引的排序等等。

判断闰年

lu@lu-VirtualBox:~$ vi leap.awk
BEGIN{
        print "pick leap years:"
}
{
        year =$1
        if((year %4 ==0 &&year % 100!=0) || year % 400==0)
                print year "is leap year"
        else
                print year " is not leap"
}
 
lu@lu-VirtualBox:~$ awk -f leap.awk year.txt 
pick leap years:
1987 is not leap
2008is leap year
3000 is not leap
2000is leap year
2012is leap year
1200is leap year
1300 is not leap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值