本文共 6800 字,大约阅读时间需要 22 分钟。
====================================
第19天 周4 20180802 授课老师-李泳谊
作者: 邢永胜====================================
awk '条件{动作}'
条件 NR==3 或 NR>=3 找出什么样的行 动作 命令 printifconfig eth0 | awk 'NR==2{print $2}' ifconfig eth0 | awk -F '[: ]+' 'NR==2{print $4}'
[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: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 反向引用
[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 此题心得: 有时想要的东西在命令的结果里,可以考虑查找命令的帮助。
已知/oldboy/test.txt文件内容为:
oldboyxizi
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
已知/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 计算统计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.txt3 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 311 显示年月日
[root@as4k /oldboy]# date +%F 2018-08-022 显示时分秒
[root@as4k /oldboy]# date +%T 15:27:103 显示年月日
[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 201808024 显示时分秒
[root@as4k /oldboy]# date +%H%M%S 15_29_29 [root@as4k /oldboy]# date +%H:%M:%S 15:29:365 显示今天日期
[root@as4k /oldboy]# date Thu Aug 2 15:29:59 CST 20186 格式化显示今天日期
[root@as4k /oldboy]# date +%F\ %T 2018-08-02 15:30:427 显示今天是周几
[root@as4k /oldboy]# date +%w 48 显示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:369 显示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:0810 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:11date [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), weekFORMAT 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
第3关剩余题目
权限转载于:https://blog.51cto.com/10308545/2153798