DAY 19

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

第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

[[email protected] ~]# 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)  

[[email protected] ~]# 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权限

[[email protected] /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}‘  

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

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

题目3 三剑客过滤空行

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

xizi

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

[[email protected] /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 命令中都表示取反。

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

题目4 三剑客取反过滤

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

[[email protected] /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的硬链接数是多少,为什么?

[[email protected] /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/..

[[email protected] /oldboy]# tree
.
├── ett
│?? ├── oldboy
│?? └── oldboy2
├── ett.txt
└── test.txt

3 directories, 2 files
[[email protected] /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/.这两个目录,还剩下
两个目录是其兄弟目录。因此通过查看目录的硬链接数也可也反推,目录中子目录的
个数。

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

简单显示系统时间和日历

[[email protected] /oldboy]# date
Thu Aug 2 01:58:52 CST 2018

[[email protected] /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 显示年月日
[[email protected] /oldboy]# date +%F
2018-08-02

2 显示时分秒
[[email protected] /oldboy]# date +%T
15:27:10

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

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

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

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

7 显示今天是周几
[[email protected] /oldboy]# date +%w
4

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

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

10 18年前
[[email protected] ~]# date +%F\ %T
2018-08-02 16:07:10
[[email protected] ~]# 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关剩余题目
权限

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

时间: 2024-08-09 03:44:43

DAY 19的相关文章

Install Adobe Flash Player 11.2 on CentOS/RHEL 7/6/5, Fedora 20/19

Adobe Flash Player are very useful for watching videos in web browser online. Without flash player most of the videos will not play in your browser. This article will help you to install Adobe flash player plugin for your browsers in CentOS 6/5, Redh

Linux内核编译 Ubuntu 14.04.3 server 升级至3.19.8

读书笔记:<Linux内核设计与实现>,原书第3版,陈莉君 康华 译 第2章:从内核出发     2.3节:编译内核 实验: ============================================================ 系统环境:VM虚拟机 Ubuntu 14.04.3 LTS server版 任务:编译安装新的内核 注意:不要跨大版本,我在3.19版本内 耗时:2小时 所有版本的内核: https://www.kernel.org/pub/linux/kernel

【网络流24题】No.19 负载平衡问题 (费用流)

[题意] G 公司有 n 个沿铁路运输线环形排列的仓库, 每个仓库存储的货物数量不等. 如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入文件示例input.txt517 9 14 16 4 输出文件示例output.txt11 [分析] 其实我觉得这题可以贪心啊..n^2贪心??.没细想.. 打的是费用流.. 大概这样建图: 懒得写了..凌乱之美.. 求满流费用.. 1 #include<cstdio> 2 #include<cstdlib&

java 19 - 9 finally有关的面试题

1 /* 2 * 面试题: 3 * 1:final,finally和finalize的区别 4 * final:最终的意思,可以修饰类,成员变量,成员方法 5 * 修饰类,类不能被继承 6 * 修饰变量,变量是常量 7 * 修饰方法,方法不能被重写 8 * finally:是异常处理的一部分,用于释放资源. 9 * 一般来说,代码肯定会执行,特殊情况:在执行到finally之前jvm退出了 10 * finalize:是Object类的一个方法,用于垃圾回收 11 * 12 * 2:如果catc

java 19 -15 File类批量更改文件名的方法

1 /* 2 需求: * 把H:\三国演义下面的视频名称修改为 3 00?_介绍.avi 4 5 思路: 6 A:封装目录 7 B:获取该目录下所有的文件的File数组 8 C:遍历该File数组,得到每一个File对象 9 D:拼接一个新的名称,然后重命名即可. 10 */ 11 package zl_file; 12 13 import java.io.File; 14 public class FileTest3 { 15 16 public static void main(String

java 19 - 3 JDK7针对多个异常的处理方式

1 /* 2 JDK7出现了一个新的异常处理方案: 3 try{ 4 5 }catch(异常名1 | 异常名2 | ... 变量 ) { 6 ... 7 } 8 如果编译期异常,又不知道异常名,就跟进看源码,那里面就能找到 9 注意:这个方法虽然简洁,但是也不够好. 10 A:处理方式是一致的.(实际开发中,好多时候可能就是针对同类型的问题,给出同一个处理) 11 B:多个异常间必须是平级关系. 12 */ 13 public class ExceptionDemo3 { 14 public s

Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server

Maven Dependencies The following Maven dependencies need to be added to the pom: <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.19.1</version> </dependency>

Spark IMF传奇行动第19课:spark排序总结

今晚听了王家林老师的Spark IMF传奇行动第19课:spark排序,作业是:1.scala 实现二次排序,使用object apply 2:自己阅读RangePartitioner 代码如下: /** * Created by 王家林 on 2016/1/10. */ object SecondarySortApp { def main(args: Array[String]){ val conf = new SparkConf() //创建SparkConf对象 conf.setAppNa

19 图形用户界面编程 - 《Python 核心编程》

?? 引言 ?? Tkinter 与Python 编程 ?? Tkinter 模块 ?? Tk 组件库 ?? Tkinter 使用举例 ?? 标签.按钮与进度条组件 ?? 一个使用 Tk 的中级范例 ?? 其他 GUI 简介(Tix, Pmw, wxPython, PyGTK) ?? 相关模块和其他 GUI Python 的默认GUI 工具集是Tk,它也是我们将使用的最基本的GUI 工具集. 我们可以通过Python 接口Tkinter 来使用Tk(Tkinter 正是“Tk 接口”之意). T

HTTP错误500.19解决方案

iis7.5详细错误   HTTP 错误 500.19 - InternalServer Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知未知 处理程序尚未确定 错误代码 0x8007052e 配置错误无法使用虚拟目录密码作为用户 Administrator 在本地登录到 C:\inetpub\wwwroot 配置文件不可用(配置隔离) 请求的 URL http://localhost:80/HL4000-Web/DefaultCh