正则表达式练习题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式练习题相关的知识,希望对你有一定的参考价值。
方法1
[[email protected] /oldboy]# ip a s eth0 |awk 'NR==3'|awk -F"[ /]+" '{print $3}'
10.0.0.200
[[email protected] ~]# ifconfig eth0 |awk 'NR==2{}'
[[email protected] ~]# ifconfig eth0 |awk 'NR==2{print $2}'
addr:10.0.0.200
方法2
[[email protected] ~]# ifconfig eth0 |awk -F'[: ]+' 'NR==2{print $4}'
10.0.0.200
[[email protected] ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:59:D4:13
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe59:d413/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:52080 errors:0 dropped:0 overruns:0 frame:0
TX packets:25292 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:61200096 (58.3 MiB) TX bytes:11691848 (11.1 MiB)
方法3
1>定位
[[email protected] ~]# ip a s eth0 |sed -n 3p
2>把ip地址之前的内容替换掉
[[email protected] ~]# ip a s eth0 |sed -n 3p |sed 's#^.*t ##g'
10.0.0.200/24 brd 10.0.0.255 scope global eth0
3>把ip地址之后的替换掉
[[email protected] ~]# ip a s eth0 |sed -n 3p |sed 's#^.*t ##g'|sed 's#/.*$##g'
10.0.0.200
命令优化
使用2个sed
[[email protected] ~]# ip a s eth0 |sed -n 3p |sed -r 's#^.*t |/.*$##g'
10.0.0.200
方法4 sed 后向引用
在前面先保护(你想要的内容),在后面通过数字使用
[[email protected] ~]# ip a s eth0 |sed -n 3p
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |sed -n 3p |sed -r 's#^.*t (.*)/.*$#1#g'
10.0.0.200
实例1-2 ip a s eth0第三行的inet替换为oldboy
[[email protected] ~]# ip a s eth0 |sed -n 3p
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |sed -n 3p|sed 's#inet#oldboy#g'
oldboy 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |egrep '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |egrep '([0-9]{1,3}.){3}[0-9]{1,3}'
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[[email protected] ~]# ip a s eth0 |egrep '([0-9]{1,3}.?){4}'
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
所有网卡的ip地址
[[email protected] ~]# hostname -I
10.0.0.200
实例1-4 取出stat /etc/hosts 文件权限 644 或 0644
方法1
awk
[[email protected] ~]# stat /etc/hosts |awk 'NR==4'
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
[[email protected] ~]# stat /etc/hosts |awk 'NR==4'|awk -F'[(/]' '{print $2}'
0644
[[email protected] ~]# stat /etc/hosts |awk -F'[(/]' 'NR==4{print $2}'
0644
方法2
sed 后向引用
[[email protected] ~]# stat /etc/hosts |sed -n 4p |sed -r 's#(^.*()([0-9]+)(/.*$)#1#g'
Access: (
[[email protected] ~]# stat /etc/hosts |sed -n 4p |sed -r 's#(^.*()([0-9]+)(/.*$)#2#g'
0644
[[email protected] ~]# stat /etc/hosts |sed -n 4p |sed -r 's#^.*(([0-9]+)/.*$#1#g'
0644
[[email protected] ~]#
方法3
[[email protected] ~]# stat -c%a /etc/hosts
644
实例1-5 已知/oldboy/test.txt文件内容为:
oldboy
xizi
xiaochao
请问如何把文件中的空行过滤掉(要求命令行实现)。
通过三剑客进行过滤
方法1grep
[[email protected] /oldboy]# grep -v '^$' test.txt
oldboy
xizi
xiaochao
方法2sed
[[email protected] /oldboy]# sed '/^$/d' test.txt
oldboy
xizi
xiaochao
方法3awk
[[email protected] /oldboy]# awk '!/^$/' test.txt
oldboy
xizi
xiaochao
实例1-6 已知/oldboy/ett.txt文件内容为:
oldboy
olldboooy
test
请使用grep或egrep正则匹配的方式过滤出前两行内容
方法1 egrep
[[email protected] /oldboy]# egrep 'ol+dbo+y' ett.txt
oldboy
olldboooy
方法2sed
[[email protected] /oldboy]# sed -nr '/ol+dbo+y/p' ett.txt
oldboy
olldboooy
方法3awk
[[email protected] /oldboy]# awk '/ol+dbo+y/' ett.txt
oldboy
olldboooy
实例1-7 linux下通过mkdir命令创建新目录/oldboy/ett,ett的硬链接数是多少,为什么?
[[email protected] /oldboy]# mkdir -p /oldboy/ett
[[email protected] /oldboy]# ll -d ett
drwxr-xr-x 2 root root 4096 Jul 20 02:01 ett
[[email protected] /oldboy]# ll -di ett ett/.
1046554 drwxr-xr-x 2 root root 4096 Jul 20 02:01 ett
1046554 drwxr-xr-x 2 root root 4096 Jul 20 02:01 ett/.
mkdir -p /oldboy/ett/oldboy
ett目录的硬连接数是?
[[email protected] /oldboy]# ll -d ett
drwxr-xr-x 3 root root 4096 Jul 20 02:05 ett
[[email protected] /oldboy]# ll -di ett ett/. ett/oldboy/..
1046554 drwxr-xr-x 3 root root 4096 Jul 20 02:05 ett
1046554 drwxr-xr-x 3 root root 4096 Jul 20 02:05 ett/.
1046554 drwxr-xr-x 3 root root 4096 Jul 20 02:05 ett/oldboy/..
[[email protected] /oldboy]# date +%F
2018-07-20
[[email protected] /oldboy]# date +%Y-%m-%d
2018-07-20
[[email protected] /oldboy]# date +%T
02:29:22
[[email protected] /oldboy]# date +%H:%M:%S
02:29:46
[[email protected] /oldboy]# date +%w
5
小结:
%F %Y-%m-%d 年-月-日
%T %H:%M:%S 时:分:秒
%w 周几
实例1-9 显示当前日期格式: 年月日_小时
[[email protected] /oldboy]# date +%F
2018-07-20
[[email protected] /oldboy]# date -d "1 day ago"
Thu Jul 19 02:41:01 CST 2018
[[email protected] /oldboy]# date -d "1day"
Sat Jul 21 02:41:15 CST 2018
[[email protected] /oldboy]# date -d "-1day"
Thu Jul 19 02:41:28 CST 2018
[[email protected] /oldboy]# date -d "-1day" +%F
2018-07-19
date 显示系统时间
-d 根据你的描述显示指定日期
-d '-7day' 7天之前
-d '7day' 7天之后
-d '+7day' 7天之后
以上是关于正则表达式练习题的主要内容,如果未能解决你的问题,请参考以下文章