码迷,mamicode.com
首页 > 其他好文 > 详细

DAY 19

时间:2018-08-03 01:14:08      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:gid   eth0   --   pre   play   modify   egrep   ethernet   总结   

====================================

第19天 周4 20180802 授课老师-李泳谊
作者: 邢永胜

====================================

awk扩展

awk ‘条件{动作}‘
条件 NR==3 或 NR>=3 找出什么样的行
动作 命令 print

ifconfig eth0 | awk ‘NR==2{print $2}‘  
ifconfig eth0 | awk -F ‘[: ]+‘ ‘NR==2{print $4}‘  

题目1 取出网卡ip

[root@as4k ~]# ifconfig eth0  
eth0      Link encap:Ethernet  HWaddr 00:0C:29:02:4B:F5    
        inet addr:192.168.56.11  Bcast:192.168.56.255  Mask:255.255.255.0  
        inet6 addr: fe80::20c:29ff:fe02:4bf5/64 Scope:Link  
        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
        RX packets:311780 errors:0 dropped:0 overruns:0 frame:0  
        TX packets:248323 errors:0 dropped:0 overruns:0 carrier:0  
        collisions:0 txqueuelen:1000   
        RX bytes:82606035 (78.7 MiB)  TX bytes:142937754 (136.3 MiB)  

[root@as4k ~]# ip a s eth0  
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 00:0c:29:02:4b:f5 brd ff:ff:ff:ff:ff:ff  
    inet 192.168.56.11/24 brd 192.168.56.255 scope global eth0  
    inet6 fe80::20c:29ff:fe02:4bf5/64 scope link   
    valid_lft forever preferred_lft forever  

ifconfig eth0 | awk ‘NR==2‘ | sed ‘s#^.*r:##g‘ | sed ‘s#Bc.*##g‘  
ifconfig eth0 | awk -F ‘[: ]+‘ ‘NR==2{print $4}‘  
ifconfig eth0 | sed -n 2p | sed -r ‘s#(.*addr:)|(Bc.*$)##g‘  
# ★★★  
ip a s eth0 | awk -F ‘[ /]+‘ ‘NR==3{print $3}‘  
ip a s eth0 | sed -n 3p   | sed ‘s#^.*t ##g‘ | sed ‘s#/.*$##g‘  
ip a s eth0 | sed -n 3p   | sed -r ‘s#^.*t |/.*$##g‘  
ip a s eth0 | sed -n 3p   | sed -r ‘s#(^.*t )|(/.*$)##g‘  
ip a s eth0 | awk ‘NR==3‘ | sed -r ‘s#(^.*t )|(/.*$)##g‘  
# 反向引用方法取IP  
ifconfig eth0 | awk ‘NR==2‘ | sed -r ‘s#(^.*addr:)(.*)(Bca.*$)#\2#g‘  
ifconfig eth0 | awk ‘NR==2‘ | sed -r ‘s#^.*addr:(.*)Bca.*$#\1#g‘  
ifconfig eth0 | awk ‘NR==2‘ | sed -r ‘s#(^.*addr:)(.*)(  Bca.*$)#\2#g‘ | cat -A  
# ★★★  
ip a s eth0   | awk ‘NR==3‘ | sed -r ‘s#(^.*t )(.*)(/.*$)#\2#g‘  
ip a s eth0 | sed -n ‘3p‘ | sed ‘s#inet#oldboy#g‘  
ip a s eth0 | sed -n ‘3s#inet#oldboy#gp‘  
ip a s eth0 | sed -nr ‘3s#(^.*t )|(/.*$)##gp‘  
# hostname加参数直接取得  
hostname -I  
hostname -i (需配置域名解析)  

## 取出IP地址小结  
    1. awk 指哪打哪  
    2. sed 使用正则  
    3. sed 反向引用  

题目2 取出stat /etc/hosts权限

[root@as4k /as4k]# stat /etc/hosts | nl
1 File: `/etc/hosts‘
2 Size: 179 Blocks: 8 IO Block: 4096 regular file
3 Device: 803h/2051d Inode: 915740 Links: 2
4 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
5 Access: 2018-08-01 19:41:38.662386294 +0800
6 Modify: 2018-07-25 14:48:45.037907762 +0800
7 Change: 2018-07-25 14:48:45.045908338 +0800

#解答:  
stat /etc/hosts | awk ‘NR==4‘ | awk -F "[(/]"  ‘{print $2}‘  
stat /etc/hosts | awk -F ‘[0/]‘ ‘NR==4{print $2}‘  
stat /etc/hosts | sed -nr ‘4s#(^Access: \(0)|(/.*$)##gp‘  
stat /etc/hosts | awk -F ‘[(/]‘ ‘NR==4{print $2}‘  
stat /etc/hosts | sed -nr ‘4s#(^Access: \(0)(.*)(/-.*$)#\2#gp‘  
stat /etc/hosts | sed -nr ‘4s#(^Access: \()(.*)(/-.*$)#\2#gp‘  
stat /etc/hosts | awk ‘NR==4‘ | sed -r ‘s#^.*\(([0-9]+).*$#\1#g‘  
stat /etc/hosts | sed -nr ‘4s#^.*\(([0-9]+).*$#\1#gp‘  
stat /etc/hosts | sed -nr ‘4s#^.*0([0-9]+).*$#\1#gp‘  
stat oldboy.txt | awk ‘NR==4‘ | awk -F "[(/]"  ‘{print $2}‘  

[root@as4k /as4k]# stat -c %A /etc/hosts  
-rw-r--r--  
[root@as4k /as4k]# stat -c %a /etc/hosts  
644  

此题心得:  
    有时想要的东西在命令的结果里,可以考虑查找命令的帮助。  

题目3 三剑客过滤空行

已知/oldboy/test.txt文件内容为:
oldboy

xizi

xiaochao
请问如何把文件中的空行过滤掉(要求命令行实现)。

[root@as4k /oldboy]# cat -nA test.txt
1 oldboy$
2 $
3 xizi$
4 $
5 xiaochao$

解答:  
grep -v ‘^$‘ test.txt  
sed ‘/^$/d‘  test.txt  (按行为单位删除)  
awk ‘!/^$/‘  test.txt  
sed -n ‘/^$/!p‘ test.txt  

!在awk sed find 命令中都表示取反。

[root@as4k /oldboy]# grep -v ‘^$‘ test.txt   
oldboy  
xizi  
xiaochao  
[root@as4k /oldboy]# awk ‘!/^$/‘ test.txt   
oldboy  
xizi  
xiaochao  
[root@as4k /oldboy]# sed ‘/^$/d‘ test.txt   
oldboy  
xizi  
xiaochao  

题目4 三剑客取反过滤

已知/oldboy/ett.txt文件内容为:
oldboy
olldboooy
test
请使用grep或egrep正则匹配的方式过滤出前两行内容

[root@as4k /oldboy]# cat ett.txt
oldboy
olldboooy
test

解答:

grep -v test ett.txt  
sed ‘/test/d‘ ett.txt  
sed -n ‘/test/!p‘ ett.txt  
awk ‘!/test/‘ ett.txt  
grep  -o ‘o.*y‘ ett.txt  
egrep ‘oldboy|olldboooy‘ ett.txt  
egrep ‘ol+dbo+y‘ ett.txt  

三剑客过滤小结

grep 过滤 显示执行过程-o 上色
sed 过滤 替换 修改文件
awk 过滤 取列-F 计算统计

题目5 目录的硬链接

linux下通过mkdir命令创建一个新目录/oldboy/ett,
ett的硬链接数是多少,为什么?

[root@as4k /oldboy]# ls -lidh ett ett/. ett/oldboy/..
664237 drwxr-xr-x 3 root root 4.0K Aug 2 01:39 ett
664237 drwxr-xr-x 3 root root 4.0K Aug 2 01:39 ett/.
664237 drwxr-xr-x 3 root root 4.0K Aug 2 01:39 ett/oldboy/..

[root@as4k /oldboy]# tree
.
├── ett
│?? ├── oldboy
│?? └── oldboy2
├── ett.txt
└── test.txt

3 directories, 2 files
[root@as4k /oldboy]# ls -lidh ett ett/. ett/oldboy/.. ett/oldboy2/..
664237 drwxr-xr-x 4 root root 4.0K Aug 2 01:43 ett
664237 drwxr-xr-x 4 root root 4.0K Aug 2 01:43 ett/.
664237 drwxr-xr-x 4 root root 4.0K Aug 2 01:43 ett/oldboy/..
664237 drwxr-xr-x 4 root root 4.0K Aug 2 01:43 ett/oldboy2/..

可以看出,ett目录的硬链接目录数是4,扣除ett和ett/.这两个目录,还剩下
两个目录是其兄弟目录。因此通过查看目录的硬链接数也可也反推,目录中子目录的
个数。

# 过滤目录中子目录的个数  
[root@as4k /oldboy]# ls -l /etc | grep ‘^d‘ | wc -l  
77  
[root@as4k /oldboy]# ls -lidh /etc/  
915713 drwxr-xr-x. 79 root root 4.0K Jul 29 00:37 /etc/  

简单显示系统时间和日历

[root@as4k /oldboy]# date
Thu Aug 2 01:58:52 CST 2018

[root@as4k /oldboy]# cal
August 2018
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

按照要求的格式显示日期

示例

1 显示年月日
[root@as4k /oldboy]# date +%F
2018-08-02

2 显示时分秒
[root@as4k /oldboy]# date +%T
15:27:10

3 显示年月日
[root@as4k /oldboy]# date +%Y.%m.%d
2018.08.02
[root@as4k /oldboy]# date +%Y#%m#%d
2018#08#02
[root@as4k /oldboy]# date +%Y%m%d
20180802

4 显示时分秒
[root@as4k /oldboy]# date +%H%M%S
15_29_29
[root@as4k /oldboy]# date +%H:%M:%S
15:29:36

5 显示今天日期
[root@as4k /oldboy]# date
Thu Aug 2 15:29:59 CST 2018

6 格式化显示今天日期
[root@as4k /oldboy]# date +%F\ %T
2018-08-02 15:30:42

7 显示今天是周几
[root@as4k /oldboy]# date +%w
4

8 显示1天前
[root@as4k ~]# date +%F\ %T
2018-08-02 16:04:35
[root@as4k ~]# date -d ‘-1 day‘ +%F\ %T
2018-08-01 16:04:36

9 显示5天后
[root@as4k ~]# date +%F\ %T
2018-08-02 16:06:07
[root@as4k ~]# date -d ‘+5 day‘ +%F\ %T
2018-08-07 16:06:08

10 18年前
[root@as4k ~]# date +%F\ %T
2018-08-02 16:07:10
[root@as4k ~]# date -d ‘-18 year‘ +%F\ %T
2000-08-02 16:07:11

抽象

date [OPTION]... [+FORMAT]

Display the current time in the given FORMAT

-d, --date=STRING
Display time described by string STRING,
as opposed to the default, which is ‘now‘.
可接受类型类似如下:
+1 year 1年后
-1 year 1年前
+3 day 3天后
-3 day 3天前
month, year(s), day(s), week

FORMAT controls the output. Interpreted sequences are:

%F     full date; same as %Y-%m-%d  
%T     time; same as %H:%M:%S  
%Y     year  
%m     month (01..12)  
%d     day of month (e.g, 01)  
%H     hour (00..23)  
%M     minute (00..59)  
%S     second (00..60)  

Relative items in date strings
https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html#Relative-items-in-date-strings

总结-19天

  1. 正则表达式,取IP地址,取出权限
  2. 三剑客进行过滤与区别
  3. 目录的硬链接数量
  4. 显示日期,时间

预告-21天

第3关剩余题目
权限

DAY 19

标签:gid   eth0   --   pre   play   modify   egrep   ethernet   总结   

原文地址:http://blog.51cto.com/10308545/2153798

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!