第1章 回顾昨天
1.1 特殊符号-通配符-基础正则表达式
&& 并且 前面命令成功再执行后面的
|| 或者 前面命令执行失败了再执行后面的
你有女朋友 || 找一个
[root@oldboyedu-40-nb ~]# echo 错误 || echo wrong
错误
[root@oldboyedu-40-nb ~]# #不贰过
[root@oldboyedu-40-nb ~]# ech 错误 || echo wrong
-bash: ech: command not found
wrong
>> 追加
> 标准输出重定向 会清空源文件内容
/ 根目录 路径分隔符
$ 取变量内容
$() `` 反引号
. 当前目录
.. 当前目录的上一级目录
~ 当前用户的家目录
- oldboy的家目录?
[root@oldboyedu-40-nb ~]# ls -ld ~oldboy
drwx------. 2 oldboy oldboy 4096 Aug 17 18:21 /home/oldboy
| 管道
! 取反 find awk
使用历史命令======history
# 注释
1.2 通配符
找出文件
* {}
1.3 基础正则表达式
找出文件的内容
三剑客grep sed awk
^
$
^$
.*
[abc] 一个整体 a或b或c
正则表达式认为只要是在中括号里面的 就是一样的.
[^abc]
http://lidao.blog.51cto.com/3388056/1941482
第2章 扩展正则表达式
2.1 + 前一个字符连续出现了1次或1次以上
2.1.1 连续出现 -- 重复
[root@oldboyedu-40-nb oldboy]# egrep "0+" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu-40-nb oldboy]# egrep -o "0+" oldboy.txt
000
00000
一出手就是专业的规范的.
示范:
egrep "0*" oldboy.txt 零次的时候 egrep "" oldboy.txt ==>把整个文件的内容显示出来
egrep "0+" oldboy.txt
* 连续出现 0次或0次以上 >= 0
+ >= 1
[root@oldboyedu-40-nb oldboy]# egrep "0*" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
||||||
[root@oldboyedu-40-nb oldboy]# egrep "0+" oldboy.txt
my qq num is 49000448.
not 4900000448.
grep -o grep命令每次找到的东西 grep执行过程 但是不会显示空行
2.2 练习题
2.2.1 取出文件中连续出现的大写字母取出来
[root@oldboyedu-40-nb oldboy]# ##取出文件中连续出现的大写字母取出来
[root@oldboyedu-40-nb oldboy]# ###1.如何取出大写字母
[root@oldboyedu-40-nb oldboy]# grep "[A-Z]" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# grep -o "[A-Z]" oldboy.txt
I
I
I
O
L
D
B
O
Y
[root@oldboyedu-40-nb oldboy]# ####2.连续出现的大写字母
[root@oldboyedu-40-nb oldboy]# egrep "[A-Z]+" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep -o "[A-Z]+" oldboy.txt
I
I
I
OLDBOY
或者
[root@oldboyedu-40-nb oldboy]# egrep "oldboy|oldbey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep "3306|1521" /etc/services
mysql 3306/tcp # MySQL
mysql 3306/udp # MySQL
ncube-lm 1521/tcp # nCube License Manager
ncube-lm 1521/udp # nCube License Manager
[root@oldboyedu-40-nb oldboy]#
[root@oldboyedu-40-nb oldboy]# egrep "[ABC]" oldboy.txt
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep "A|B|C" oldboy.txt
my god ,i am not oldbey,but OLDBOY!
2.2.2 () 小括号里面的内容是一个整体,相当于是一个字符 反向引用(sed )
[root@oldboyedu-40-nb oldboy]# egrep "oldboy|oldbey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep "oldbo|ey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep "oldb(o|e)y" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
括号
() 小括号
[] 中括号
{} 大括号 花括号
2.2.3 反向引用
1.先把你要的内容保护起来 ...(你要的内容)...
2.再使用 \1 \2 \3
[root@oldboyedu-40-nb oldboy]# echo 123456
123456
[root@oldboyedu-40-nb oldboy]# echo 34
34
[root@oldboyedu-40-nb oldboy]# echo 123456|sed 's#12|56##g'
123456
[root@oldboyedu-40-nb oldboy]# echo 123456|sed -r 's#12|56##g'
34
[root@oldboyedu-40-nb oldboy]# echo 123456|sed -r 's#..(..)..#\1#g'
34
[root@oldboyedu-40-nb oldboy]# echo 123456|sed -r 's#(.).(..).(.)#\1#g'
1
[root@oldboyedu-40-nb oldboy]# echo 123456|sed -r 's#(.).(..).(.)#\2#g'
34
[root@oldboyedu-40-nb oldboy]# echo 123456|sed -r 's#(.).(..).(.)#\3#g'
6
echo 101010 | sed -r 's#(.)(.)(.)#\1----\2-----\3#g'
echo 123456 | sed -r 's#(.)(.)(.)#\1----\2-----\3#g'
echo 123456 | sed -r 's#(.)(.)(.)#\1#g'
[root@oldboyedu-40-nb oldboy]# echo 123456 | sed -r 's#(.)(.)(.)#\1#g'
14
[root@oldboyedu-40-nb oldboy]# echo 123456 | sed -r 's#(.)(.)(.).#\1#g'
156
[root@oldboyedu-40-nb oldboy]# echo 123456 | sed -r 's#(.)(.)(.).##g'
56
[root@oldboyedu-40-nb oldboy]# echo 123456 | sed -r 's#(.)(.)(.).#lidao \1 lidao #g'
lidao 1 lidao 56
2.2.4 {} 0{n,m} 数字0连续出现了至少n次,最多m次
* 连续 >=0
+ 连续 >=1
取出文件中连续出现3-6次的字母
1.字母
[root@oldboyedu-40-nb oldboy]# grep "[a-zA-Z]" oldboy.txt
[root@oldboyedu-40-nb oldboy]# grep "[a-Z]" oldboy.txt
[root@oldboyedu-40-nb oldboy]# grep -i "[a-z]" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
2.连续
[root@oldboyedu-40-nb oldboy]# egrep "[a-zA-Z]{3,6}" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# egrep "[a-zA-Z]{3,6}" oldboy.txt -o
0{3,6} >=3 <=6
0{3} ==3
0{3,} >=3
0{,6} >=0 <=6
2.2.5 ? 前一个字符连续出现了 0次或1次 ==0 ==1
环境:
a.log
good
gd
god
goood
测试:
[root@oldboyedu-40-nb oldboy]# cat a.log
good
gd
god
goood
[root@oldboyedu-40-nb oldboy]# egrep "gd|god" a.log
gd
god
[root@oldboyedu-40-nb oldboy]# egrep "go?d" a.log
gd
god
2.3 小结:
基础正则
^ $ ^$ .* [abc] [a-z] [A-Z] [^abc] [^香辣葱]
扩展正则表达式
+ | () {} ?
基础正则与扩展正则
grep sed 基础
[root@oldboyedu-40-nb oldboy]# grep "oldboy|oldbey" oldboy.txt
[root@oldboyedu-40-nb oldboy]# egrep "oldboy|oldbey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# grep -E "oldboy|oldbey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu-40-nb oldboy]# grep "oldboy\|oldbey" oldboy.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god ,i am not oldbey,but OLDBOY!
egrep sed -r awk
第3章 正则表达式练习题
3.1 取出eth0网卡ip地址
3.1.1 定位-显示第二行
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|awk 'NR==2'
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
3.1.2 取出ip地址
方法1-sed
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed 's#^.*:##g'
255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'
10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's# Bc.*$##g'
10.0.0.200
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'|sed 's#Bc.*$##g'
10.0.0.200
方法2-sed 准备工作 找谁干啥
精简sed命令
显示出第二行的内容 替换
sed -n '2p'
找谁干啥
sed -n '2s###gp'
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#^.*dr:##gp'
10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#^.*dr:##gp'|sed 's#Bc.*$##g'
10.0.0.200
方法2-sed 除了ip地址之外的都替换为空格
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#[0-9.]##gp'
inet addr: Bcast: Mask:
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#[^0-9.]##gp'
10.0.0.20010.0.0.255255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#[^0-9.]# #gp'
10.0.0.200 10.0.0.255 255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0 |sed -n '2s#[^0-9.]# #gp'|awk '{print $1}'
10.0.0.200
ifconfig eth0 |sed -n '2s#[^0-9.]# #gp'
ifconfig eth0 |sed -n '2s#[a-zA-Z:]# #gp'
方法3-sed-反向引用
[root@oldboyedu-40-nb oldboy]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:7F:9C:5C
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe7f:9c5c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:107794 errors:0 dropped:0 overruns:0 frame:0
TX packets:75361 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:17592132 (16.7 MiB) TX bytes:25618969 (24.4 MiB)
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'
inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed 's#^.*dr:##g'
10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0
[root@oldboyedu-40-nb oldboy]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*dr:(.*) Bc.*$#\1#g'
10.0.0.200
ifconfig eth0|sed -n '2p'|sed -r 's#^.*dr:(.*) Bc.*$#\1#g'
ifconfig eth0|sed -rn '2s#^.*dr:(.*) Bc.*$#\1#gp'
3.2 小结:
1.sed 去头 去尾
2.sed-反向引用
3.3 取出 /etc/hosts文件的权限(数字)
stat /etc/hosts
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts
File: `/etc/hosts'
Size: 218 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 39 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-08-31 19:20:01.152390259 +0800
Modify: 2017-08-30 19:18:53.139883595 +0800
Change: 2017-08-30 19:18:53.139883595 +0800
方法1---掐头去尾
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts |sed -n '4s#^.*(##gp'
0/ root)
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts |sed -n '4s#^.*(0##gp'
644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts |sed -n '4s#^.*(0##gp'|sed 's#/.*$##g'
644
方法2---反向引用
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts
File: `/etc/hosts'
Size: 218 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 39 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-08-31 19:20:01.152390259 +0800
Modify: 2017-08-30 19:18:53.139883595 +0800
Change: 2017-08-30 19:18:53.139883595 +0800
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts|sed -nr '4s#^.*\(0(.*)/-.*$#\1#gp'
644
方法3---反过来想 排除
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts|sed -n '4s#[^0-7]# #gp'
0644 0 0
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts|sed -n '4s#[a-zA-Z:()/-]# #gp'
0644 0 0
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts|sed -nr '4s#[a-zA-Z:()/ -]+# #gp'
0644 0 0
[root@oldboyedu-40-nb oldboy]# stat /etc/hosts|sed -nr '4s#[^0-7]+# #gp'
0644 0 0
+ 单独的东西 合并在一起 单独的山楂 串在一起 === 糖葫芦
排除文件中的空行或包含#或只有空格的行
第4章 本周总结:
1.正则表达式
2.通配符-特殊符号
3.单引号双引号不加引号 反引号
4.文件的属性
1)linux下磁盘空间满了原因和排查过程
2)文件删除原理
3)软连接与硬链接区别
4)inode与block
5)文件的时间戳-mtime atime ctime
5.find命令 sed rm cp/mv
6.屌丝去洗浴中心之路
7.linux无法上网排查过程
8.linux如何让一个服务/脚本开机自启动
9.chkconfig管理脚本条件
考试题总结
命令总结
预习:
第三关练习题
linux权限