实例1-1 取出网卡的ip地址
方法1
[root@oldboyedu50-lnb /oldboy]# ip a s eth0 |awk 'NR==3'|awk -F"[ /]+" '{print $3}'
10.0.0.200
[root@oldboyedu50-lnb ~]# ifconfig eth0 |awk 'NR==2{}'
[root@oldboyedu50-lnb ~]# ifconfig eth0 |awk 'NR==2{print $2}'
addr:10.0.0.200
方法2
[root@oldboyedu50-lnb ~]# ifconfig eth0 |awk -F'[: ]+' 'NR==2{print $4}'
10.0.0.200
[root@oldboyedu50-lnb ~]# 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>定位
[root@oldboyedu50-lnb ~]# ip a s eth0 |sed -n 3p
2>把ip地址之前的内容替换掉
[root@oldboyedu50-lnb ~]# 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地址之后的替换掉
[root@oldboyedu50-lnb ~]# ip a s eth0 |sed -n 3p |sed 's#^.*t ##g'|sed 's#/.*$##g'
10.0.0.200
命令优化
使用2个sed
[root@oldboyedu50-lnb ~]# ip a s eth0 |sed -n 3p |sed -r 's#^.*t |/.*$##g'
10.0.0.200
方法4 sed 后向引用
在前面先保护(你想要的内容),在后面通过\数字使用
[root@oldboyedu50-lnb ~]# ip a s eth0 |sed -n 3p
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[root@oldboyedu50-lnb ~]# 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
[root@oldboyedu50-lnb ~]# ip a s eth0 |sed -n 3p
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
[root@oldboyedu50-lnb ~]# 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
实例1-3 只通过正则表达式取出ip
[root@oldboyedu50-lnb ~]# 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
[root@oldboyedu50-lnb ~]# 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
[root@oldboyedu50-lnb ~]# ip a s eth0 |egrep '([0-9]{1,3}\.?){4}'
2: eth0:
inet 10.0.0.200/24 brd 10.0.0.255 scope global eth0
所有网卡的ip地址
[root@oldboyedu50-lnb ~]# hostname -I
10.0.0.200
实例1-4 取出stat /etc/hosts 文件权限 644 或 0644
方法1
awk
[root@oldboyedu50-lnb ~]# stat /etc/hosts |awk 'NR==4'
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
[root@oldboyedu50-lnb ~]# stat /etc/hosts |awk 'NR==4'|awk -F'[(/]' '{print $2}'
0644
[root@oldboyedu50-lnb ~]# stat /etc/hosts |awk -F'[(/]' 'NR==4{print $2}'
0644
方法2
sed 后向引用
[root@oldboyedu50-lnb ~]# stat /etc/hosts |sed -n 4p |sed -r 's#(^.*\()([0-9]+)(/.*$)#\1#g'
Access: (
[root@oldboyedu50-lnb ~]# stat /etc/hosts |sed -n 4p |sed -r 's#(^.*\()([0-9]+)(/.*$)#\2#g'
0644
[root@oldboyedu50-lnb ~]# stat /etc/hosts |sed -n 4p |sed -r 's#^.*\(([0-9]+)/.*$#\1#g'
0644
[root@oldboyedu50-lnb ~]#
方法3
[root@oldboyedu50-lnb ~]# stat -c%a /etc/hosts
644
实例1-5 已知/oldboy/test.txt文件内容为:
oldboy
xizi
xiaochao
请问如何把文件中的空行过滤掉(要求命令行实现)。
通过三剑客进行过滤
方法1grep
[root@oldboyedu50-lnb /oldboy]# grep -v '^$' test.txt
oldboy
xizi
xiaochao
方法2sed
[root@oldboyedu50-lnb /oldboy]# sed '/^$/d' test.txt
oldboy
xizi
xiaochao
方法3awk
[root@oldboyedu50-lnb /oldboy]# awk '!/^$/' test.txt
oldboy
xizi
xiaochao
实例1-6 已知/oldboy/ett.txt文件内容为:
oldboy
olldboooy
test
请使用grep或egrep正则匹配的方式过滤出前两行内容
方法1 egrep
[root@oldboyedu50-lnb /oldboy]# egrep 'ol+dbo+y' ett.txt
oldboy
olldboooy
方法2sed
[root@oldboyedu50-lnb /oldboy]# sed -nr '/ol+dbo+y/p' ett.txt
oldboy
olldboooy
方法3awk
[root@oldboyedu50-lnb /oldboy]# awk '/ol+dbo+y/' ett.txt
oldboy
olldboooy
实例1-7 linux下通过mkdir命令创建新目录/oldboy/ett,ett的硬链接数是多少,为什么?
[root@oldboyedu50-lnb /oldboy]# mkdir -p /oldboy/ett
[root@oldboyedu50-lnb /oldboy]# ll -d ett
drwxr-xr-x 2 root root 4096 Jul 20 02:01 ett
[root@oldboyedu50-lnb /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目录的硬连接数是?
[root@oldboyedu50-lnb /oldboy]# ll -d ett
drwxr-xr-x 3 root root 4096 Jul 20 02:05 ett
[root@oldboyedu50-lnb /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/..
实例1-8 按照要求的格式显示日期
[root@oldboyedu50-lnb /oldboy]# date +%F
2018-07-20
[root@oldboyedu50-lnb /oldboy]# date +%Y-%m-%d
2018-07-20
[root@oldboyedu50-lnb /oldboy]# date +%T
02:29:22
[root@oldboyedu50-lnb /oldboy]# date +%H:%M:%S
02:29:46
[root@oldboyedu50-lnb /oldboy]# date +%w
5
小结:
%F %Y-%m-%d 年-月-日
%T %H:%M:%S 时:分:秒
%w 周几
实例1-9 显示当前日期格式: 年月日_小时
[root@oldboyedu50-lnb /oldboy]# date +%F
2018-07-20
[root@oldboyedu50-lnb /oldboy]# date -d "1 day ago"
Thu Jul 19 02:41:01 CST 2018
[root@oldboyedu50-lnb /oldboy]# date -d "1day"
Sat Jul 21 02:41:15 CST 2018
[root@oldboyedu50-lnb /oldboy]# date -d "-1day"
Thu Jul 19 02:41:28 CST 2018
[root@oldboyedu50-lnb /oldboy]# date -d "-1day" +%F
2018-07-19
date 显示系统时间
-d 根据你的描述显示指定日期
-d '-7day' 7天之前
-d '7day' 7天之后
-d '+7day' 7天之后