时间:2021-11-19 13:21:40 | 栏目:Shell | 点击:次
这三个字符用来限制关键词的匹配次数,含义分别如下:
准备测试文件: [root@svr5 ~]# cat tel.txt 01012315 137012345678 13401234567 10086 18966677788 提取包含11位手机号的行: [root@svr5 ~]# egrep '^1[0-9]{10}$' tel.txt 13401234567 18966677788
作用:地址符(执行指令的条件)控制sed需要处理文本的范围;不加定址符则逐行处理所有行
表示方式:地址符可以使用行号或正则表达式
查看测试文本:
[root@svr5 ~]# cat -n /etc/rc.local 1 #!/bin/sh 2 # 3 # This script will be executed *after* all the other init scripts. 4 # You can put your own initialization stuff in here if you don't 5 # want to do the full Sys V style init stuff. 6 7 touch /var/lock/subsys/local
提取偶数行的操作及效果:
[root@svr5 ~]# cat -n /etc/rc.local | sed -n '2~2p' 2 # 4 # You can put your own initialization stuff in here if you don't 6
查看测试文本:
[root@svr5 ~]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local
删除每行第4个字符的操作及效果:
[root@svr5 ~]# cat /etc/rc.local | sed 's/.//4' #!/in/sh # # Tis script will be executed *after* all the other init scripts. # Yu can put your own initialization stuff in here if you don't # wnt to do the full Sys V style init stuff. touh /var/lock/subsys/local
提取或导出文本:
[root@svr5 ~]# sed -n '6,10p' /etc/passwd > pass5.txt
确认提取结果:
[root@svr5 ~]# cat pass5.txt sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
格式1: awk [选项] ‘[条件]{处理动作}' 文件列表
格式2: 命令 | awk [选项] ‘[条件]{处理动作}'
查看测试文本:
[root@svr5 ~]# ip add list eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:64:88:8e brd ff:ff:ff:ff:ff:ff inet 192.168.4.55/24 brd 192.168.4.255 scope global eth0 inet 192.168.4.5/24 brd 192.168.4.255 scope global secondary eth0 inet6 fe80::20c:29ff:fe64:888e/64 scope link valid_lft forever preferred_lft forever
提取IPv4地址及掩码信息的操作及效果:
[root@svr5 ~]# ip add list eth0 | awk '/\<inet\>/{print $2}' 192.168.4.55/24 192.168.4.5/24
[root@svr5 ~]# awk -F: '$3>=10 && $3<=20{print $1":"$3}' /etc/passwd uucp:10 operator:11 games:12 gopher:13 ftp:14
使用NF内置变量找最后一列的内容,匹配bash即可让x+1:
[root@svr5 ~]# awk -F/ '$NF=="bash"{x++}END{print x}' /etc/passwd
可以使用数组,分别以 数组名、下标、值 三个部分构成
使用sort命令,比如abc.txt文本
另外还可以使用选项-n按数字升序排列 -k:针对指定的列进行排序 -r:反向排序
[root@svr5 ~]# sort ?Cn abc.txt