老男孩Linux运维第41期20171016第六周学习重点课堂记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了老男孩Linux运维第41期20171016第六周学习重点课堂记录相关的知识,希望对你有一定的参考价值。
Week6课堂知识点总结 |
作者:Old_Pan 归档:学习笔记 2017/10/16
|
目 录
第1章 正则表达式与通配符
1.1 特殊符号
特殊符号 | 作用 |
单引号‘‘ | 对括号中的内容不作任何解析,所见即所得,吃啥吐啥 |
双引号"" | 和单引号类似,但是特殊符号会被解析 运行 $ $() `` ! |
反引号`` | === $() 先运行里面的命令,然后把显示到屏幕上的内容留下.. |
# | 表示注释以及root用户命令提示符 |
$ | 1.取变量里面的内容,例:$LANG、$PATH 2.普通用户的命令提示符 3.sed -n ‘$p‘ oldboy.txt 4.awk $1 $2 $NF 取某一列 5.NR----number of record(行号) |
! | 1.使用运行历史命令history,例:!n,n是第几条历史命令 2.find awk表示取反、非 |
* | 通配符,表示匹配一切 |
&& | 前一个命令运行成功,再运行后面的命令,例:ifdown eth0 && ifup eth0 |
|| | 前一个命令运行失败,再运行后面的命令 |
| | 管道 |
? | 通配符,表示匹配任意一个字符 |
\ | 1.转义符号 2.临时取消别名 |
1.2 通配符
通配符作用:用来匹配文件名
* 所有、任何东西
找出以 *.txt*.log
ls -l *.txt *.log
找出/oldboy 目录中 文件名中包含oldboy的文件
mkdir -p/oldboy touch/oldboy/oldboy{01..10}.txt /oldboy/stu-oldboy{01..10}.txt oldboy oldboy.txt find/oldboy/ -name *oldboy*
1.3 {} 生成序列
#生成序列
echo {a..z}
echo {1..10}
echo {01..10}
echo {001..10}
echo {01..10}
echo {01..100}
#指定每次变化多少
[[email protected] tmp]# echo {01..10..2} 01 03 05 07 09 [[email protected] tmp]# echo {a..z..2} a c e g i k m o q s u w y [[email protected] tmp]# echo stu{01,05,10} stu01 stu05 stu10
#通过{}对某个文件进行备份
[[email protected] oldboy]# echo A{B,C} AB AC [[email protected] oldboy]# echo A{,C} A AC [[email protected] oldboy]# [[email protected] oldboy]# echo oldboy.txt{,C} oldboy.txt oldboy.txtC [[email protected] oldboy]# echo oldboy.txt{,.bak} oldboy.txt oldboy.txt.bak [[email protected] oldboy]# touch oldboy.txt [[email protected] oldboy]# cp oldboy.txt{,.bak} [[email protected] oldboy]# #cp oldboy.txt{,.bak} [[email protected] oldboy]# #cp oldboy.txt oldboy.txt.bak [[email protected] oldboy]# echo oldboy.txt{,.bak} oldboy.txt oldboy.txt.bak
1.4 正则表达式
1.4.1 什么是正则
又称规则表达式。(英语:RegularExpression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。
创建试验环境:
cat > test.txt <<EOF oldboy 230xxxxxxxxxxxxxxxxxxx oldboy01 oldboy43.txt oldboy-lidao.txt oldboybaby oldbabay EOF
找出文件中包含数字的行
grep "[0-9]" test.txt
通过一种符合表示不同的数字 字母 ====== 正则表达式(特殊符号 ^ $ + . * 表示各种不同的字符)
1.4.2 使用正则的时候注意事项
1# 正则表达式是高级货色linux三剑客 grep sed awk
2# 正则表达式匹配按照行为单位的
grep "^oldboy" oldboy.txt
3# 使用的时候注意 使用英文符号, 禁止使用中文符号
4# 给grep/egrep 加上一个别名可以增加颜色区分显示结果
cat >>/etc/profile<<EOF alias grep=‘grep --color=auto‘ alias egrep=‘egrep --color=auto‘ EOF source /etc/profile [[email protected] oldboy]# alias grep egrep alias grep=‘grep --color=auto‘ alias egrep=‘egrep --color=auto‘
1.4.3 正则表达式与通配符区别
1# 正则表达式高级货色 查找文件内容 文字符号 grep sed awk 三剑客支持 python java
2# 通配符 查找文件名字大部分linux命令都可以使用 通配符 *.txt *.log
1.4.4 正则表达式分类
1、 基础正则 ^ $ . * [] basic regular expression BRE
2、扩展正则 + | (){} ? extended regular expression ERE
1.4.5 准备测试环境
[[email protected] oldboy]# cat -noldboy.txt 1 I am oldboy teacher! 2 I teach linux. 3 4 I like badminton ball ,billiardball and chinese chess! 5 my blog ishttp://oldboy.blog.51cto.com 6 our site ishttp://www.etiantian.org 7 my qq num is 49000448. 8 9 not 4900000448. 10 my god ,i am not oldbey,butOLDBOY!
1.4.6 基础正则
1.4.6.1 ^ 表示以....开头的行
[[email protected] oldboy]# grep"^m" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. my god ,i am not oldbey,but OLDBOY!
1.4.6.2 $ 以....结尾的行
cat -A可以在文件每一行最后加上结尾符$
[[email protected] oldboy]# cat -Aoldboy.txt I am oldboy teacher!$ I teach linux.$ $ I like badminton ball ,billiard ball andchinese 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!$ [[email protected] oldboy]# grep"m$" oldboy.txt my blog is http://oldboy.blog.51cto.com
1.4.6.3 空行 ^$ 这一行中没有任何符号
[[email protected] oldboy]# grep -n"^$" oldboy.txt 3: 8: [[email protected] oldboy]# cat -noldboy.txt 1 I am oldboy teacher! 2 I teach linux. 3 4 I like badminton ball ,billiardball and chinese chess! 5 my blog ishttp://oldboy.blog.51cto.com 6 our site ishttp://www.etiantian.org 7 my qq num is 49000448. 8 9 not 4900000448. 10 my god ,i am not oldbey,butOLDBOY!
#排除空行
[[email protected] oldboy]# grep -v"^$" oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese 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!
1.4.6.4 .(点) 匹配任意一个字符但不匹配空行
[[email protected] oldboy]# #grep -o "." oldboy.txt [[email protected] oldboy]# #-o grep命令每次 匹配到了什么 [[email protected] oldboy]# grep -o "." oldboy.txt
1.4.6.5 \ 转义字符 脱掉马甲 打回原形
#找出文件中oldboy.txt 以.结尾的行
[[email protected] oldboy]# grep"\.$" oldboy.txt I teach linux. my qq num is 49000448. not 4900000448.
\n 回车换行
#^ $ ^$ .
#grep -o 显示grep命令每次找到的内容
1.4.6.6 * 前一个字符连续出现0次或1次以上 >=0
[[email protected] oldboy]# grep"0" oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# grep -o"0" oldboy.txt 0 0 0 0 0 0 0 0 [[email protected] oldboy]# grep -on"0" oldboy.txt 7:0 7:0 7:0 9:0 9:0 9:0 9:0 9:0
[[email protected] oldboy]# grep"0*" oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese 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! [[email protected] oldboy]# grep -o"0*" oldboy.txt 000 00000
连续出现
0
0000
000000
####正则中表示连续出现或所有的时候 ----- 贪婪性
#"0*" 为何会把整个文件的内容显示出来
#0连续出现了1次以上 000 00000 0000000
#0连续出现了0次 会把整个文件的内容都显示出来"0*" ==== ""
grep "" oldboy.txt
I am oldboy teacher!
I teach linux.
Ilike 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!
1.4.6.7 .* 所有任何东西
[[email protected] oldboy]# grep -n"^.*o" oldboy.txt 1:I am oldboy teacher! 4:I like badminton ball ,billiard ball andchinese chess! 5:my blog is http://oldboy.blog.51cto.com 6:our site is http://www.etiantian.org 9:not 4900000448. 10:my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# # #正则中表示连续出现或所有的时候 ----- 贪婪性 [[email protected] oldboy]# #有多少匹配多少有多少吃多少
#我要找以m开头的行 并且 以m结尾的行
[[email protected] oldboy]# ##我要找以m开头的行并且 以m结尾的行 [[email protected] oldboy]# grep"^m" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# grep"^m$" oldboy.txt [[email protected] oldboy]# #grep"^m什么都可以m$"oldboy.txt [[email protected] oldboy]# grep "^m什么都可以m$" oldboy.txt [[email protected] oldboy]# grep"^m.*m$" oldboy.txt my blog is http://oldboy.blog.51cto.com
1.4.6.8 [] [abc] 表示一个整体相当于是一个符号 表示包含a或者b或者c的行
grep "[abc]" oldboy.txt
grep "[a-z]" oldboy.txt
grep "[A-Z]" oldboy.txt
grep "[a-zA-Z]" oldboy.txt
grep "[0-9]" oldboy.txt
grep "[a-zA-Z0-9]" oldboy.txt
练习3:以 m或n或o开头的 并且以 m或g 结尾的行
[[email protected] oldboy]# grep"^[mno]" oldboy.txt 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! [[email protected] oldboy]# grep "^[mno].*[mg]$"oldboy.txt my blog is http://oldboy.blog.51cto.com our site is http://www.etiantian.org
[[email protected] tmp]# grep"[a-Z]" oldboy.txt
[[email protected] oldboy]# grep"^[m|n|o].*[m|g]$" oldboy.txt [[email protected] oldboy]# grep"^[m,n,o].*[m,g]$" oldboy.txt [[email protected] oldboy]# #在正则中括号中 特殊符号(正则表达式符号) 一般都没有特殊含义 [[email protected] oldboy]# #grep"\.$" [[email protected] oldboy]# #grep"[.]$"
问题:- 表示非特殊符号 在这里怎么是从a到z啦?
1.4.6.9 [^] [^abc] 表示一个整体相当于是一个符号 表示包含a或者b或者c的行
第一个位置 排除
[[email protected] oldboy]# grep"[xlc]" oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese chess! my blog is http://oldboy.blog.51cto.com my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# grep"[^xlc]" oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese 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!
[[email protected] ~]# grep"[^m^n^o].*[m$g$]" oldboy.txt
[^m^n^o]表示 : 排除m或^或n或o
[[email protected] oldboy]# grep"[^m-m$]" oldboy.txt
1.4.6.10 基础正则表达式小结:
(1) .^ $ ^$ .* [abc]
(2) grep grep -o -o参数把找到的东西显示出来
1.4.7 扩展正则ERE
1.4.7.1 + 前一个字符连续出现1次或多次 >=1
[[email protected] oldboy]# grep"0+" oldboy.txt [[email protected] oldboy]# egrep"0+" oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# #egrep ===grep -E [[email protected] oldboy]# egrep"0+" oldboy.txt -o 000 00000 [[email protected] oldboy]# #取出文件中连续出现的小写字母? [[email protected] oldboy]# egrep‘[a-z]+‘ oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese 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! -----oooo [[email protected] oldboy]# egrep ‘[a-z]+‘oldboy.txt -o #连续出现 000000 00000 abcd [[email protected] oldboy]# egrep"[0-9]" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep"[0-9]" oldboy.txt -o 5 1 4 9 0 0 0 4 4 8 4 9 0 0 0 0 0 4 4 8 [[email protected] oldboy]# [[email protected] oldboy]# egrep"[0-9]+" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep"[0-9]+" oldboy.txt -o 51 49000448 4900000448 [[email protected] oldboy]# #+ 连续出现的东西 一次性取出来 变为一个整体
1.4.7.2 | 表示或者
[[email protected] oldboy]# egrep ‘a|b|c‘oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese chess! my blog is http://oldboy.blog.51cto.com our site is http://www.etiantian.org my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# egrep"[abc]" oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese chess! my blog is http://oldboy.blog.51cto.com our site is http://www.etiantian.org my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# [[email protected] 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!
1.4.7.3 () 的作用:1括号里面的内容相当于是一个整体,先算括号里面的内容;2反向引用(sed)
[[email protected] 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! [[email protected] 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! [[email protected] 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、反向引用(sed) 先保护再使用
[[email protected] oldboy]# echo 123456 123456 [[email protected] oldboy]# echo 123456 123456 [[email protected] oldboy]# echo123456|sed ‘s#.*##g‘ [[email protected] oldboy]# echo 123456|sed‘s#.*#a#g‘ a [[email protected] oldboy]##<123456> [[email protected] oldboy]# echo123456|sed -r ‘s#(.*)#\1#g‘ 123456 [[email protected] oldboy]# echo123456|sed -r ‘s#(.*)#<\1>#g‘ <123456>
[[email protected] oldboy]# echo123456|sed -r ‘s#(.*)#\1#g‘ 123456 [[email protected] oldboy]# echo123456|sed -r ‘s#(.*)#@@@\[email protected]@@@#g‘ @@@[email protected]@@@ [[email protected] oldboy]# echo123456|sed -r ‘s#(.*)#<\1>#g‘ <123456> [[email protected] oldboy]# [[email protected] oldboy]# echo123456|sed -r ‘s#..(.)...#\1#g‘ 3 [[email protected] oldboy]# echo123456|sed -r ‘s#..(.)...#\2#g‘ sed: -e expression #1, char 15: invalidreference \2 on `s‘ command‘s RHS [[email protected] oldboy]# echo123456|sed -r ‘s#(.).(.)...#\2#g‘ 3
echo 123456 |sed -r ‘s#(1)(5)#+\1+3\23#g‘
[[email protected] ~]# echo 123456|sed ‘s#(.)(.)(.)#<\2>#g‘ -r <2><5>
1.4.7.4 a{n,m} 0{n,m} 前一个字符连续出现了最少n次,最多出现了m次
* >=0
+ >=1
[[email protected] oldboy]# egrep‘0{1,3}‘ oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep‘0{1,3}‘ oldboy.txt -o 000 000 00 [[email protected] oldboy]# egrep‘0{1,3}‘ oldboy.txt -on 7:000 9:000 9:00 [[email protected] oldboy]# egrep‘0{1,3}‘ oldboy.txt [[email protected] oldboy]# egrep ‘0{2,4}‘oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep‘0{2,4}‘ oldboy.txt -o 000 0000 [[email protected] oldboy]# egrep‘0{2,4}‘ oldboy.txt -on 7:000 9:0000 #找出文件中连续出现5次到6次的字母 [[email protected] oldboy]# egrep ‘[a-z]{5,6}‘ oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese chess! my blog is http://oldboy.blog.51cto.com our site is http://www.etiantian.org my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# egrep -w ‘[a-z]{5,6}‘ oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball andchinese chess! my blog is http://oldboy.blog.51cto.com my god ,i am not oldbey,but OLDBOY! [[email protected] oldboy]# [[email protected] oldboy]# egrep -wo ‘[a-z]{5,6}‘ oldboy.txt oldboy teach linux chess oldboy oldbey [[email protected] oldboy]# egrep -won ‘[a-z]{5,6}‘ oldboy.txt 1:oldboy 2:teach 2:linux 4:chess 5:oldboy 10:oldbey [[email protected] oldboy]# egrep -woni ‘[a-z]{5,6}‘ oldboy.txt 1:oldboy 2:teach 2:linux 4:chess 5:oldboy 10:oldbey 10:OLDBOY #-w grep按照单词进行匹配(全字符精确匹配/完全匹配) grep oldboy test.txt oldboy oldboybaby testoldboy helloldboy grep -w oldboy test.txt oldboy
[[email protected] oldboy]# egrep ‘[a-z]+ {1,5}‘ oldboy.txt -o 这样会不会有冲突?
#找出文件中的QQ号码
[[email protected] oldboy]# ##找出文件中的QQ号码 [[email protected] oldboy]# egrep"[0-9]" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep"[0-9]{5,}" oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep"[0-9]{5,}" oldboy.txt -o 49000448 4900000448
#-i 不区分大小写 aA
#-w 按照单词进行匹配(全字符精确匹配/完全匹配)
#-n 显示行号
#-o 显示grep执行过程 grep正则每次匹配到的内容显示出来
[[email protected] oldboy]# egrep ‘[0-9]{5}‘ oldboy.txt my qq num is 49000448. not 4900000448. [[email protected] oldboy]# egrep ‘[0-9]{5}‘ oldboy.txt -o 49000 49000 00448
{}小结:
1. a{n,m} 前一个字符连续出现了最少n次,最多出现了m次 >=n <=m
2. a{n,} 前一个字符连续出现了最少n次 >=n
3. a{n} 前一个字符连续出现了n次 ==n
1.4.7.5 ? 前一个字符连续出现了0次或1次
[[email protected] oldboy]# egrep"go?d" a.log gd god [[email protected] oldboy]# egrep"go*d" a.log good gd god goood
1.4.8 正则表达式总结
连续出现(重复)
* >=0
+ >=1
? 0 1
{n,m} >=n <=m
其他
. 任意一个字符
[abc] 一个整体 相当于是一个字符
[a-z] [0-9] [A-Z]
[^abc] 排除
| 或者
() 后向引用 反向引用 先保护再使用
^
$
1.4.9 基础正则与扩展正则区别:
支持基础正则 | 支持基础+扩展正则 |
grep | egrep或者grep -E |
sed | sed -r |
awk |
第2章 练习题
2.1 删除掉文件中的空行
创建测试环境:
cat >/tmp/blank.txt<<EOF welcome to oldboy linux. welcome to join us. have fun. lidao.oldboyedu.com Dr.Tan EOF
#grep排除
grep-v "^$" blank.txt
egrep -v "^$|^ +$" blank.txt
egrep -v "^ *$" blank.txt
^ *$
*表示0次的时候就是 ^$
*表示1次以上的时候就是 ^多个空格$
#sed删除
sed‘找谁干啥‘ file
sed‘/^ *$/d‘ blank.txt
#awk排除 取反
awk‘/^ *$/‘ blank.txt
awk‘!/^ *$/‘ blank.txt
2.2 取出eth0网卡的ip地址
2.2.1 方法1:awk 3个管道
[[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|cut -d " " -f12 addr:10.0.0.200 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|awk ‘{print $2}‘ addr:10.0.0.200 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|awk ‘{print $2}‘|awk -F: ‘{print $2}‘ 10.0.0.200
2.2.2 方法2:awk 2个管道
[[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|awk -F "addr:" ‘{print $2}‘ 10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|awk -F "addr:|Bcast" ‘{print $2}‘ 10.0.0.200 [[email protected] oldboy]# ifconfig eth0 |awk‘NR==2‘|awk -F "addr:| Bcast"‘{print $2}‘ 10.0.0.200
2.2.3 方法3:awk 1个管道
[[email protected] oldboy]# ifconfig eth0|awk‘NR==2{print $2}‘ addr:10.0.0.200 [[email protected] oldboy]# #sed ‘找谁干啥‘ [[email protected] oldboy]# #awk ‘找谁{干啥}‘ [[email protected] oldboy]# #找谁===条件 [[email protected] oldboy]# ifconfig eth0|awk‘NR==2{print $2}‘ addr:10.0.0.200 [[email protected] oldboy]# ifconfig eth0 |awk-F "addr:| Bcast" ‘NR==2{print$2}‘ 10.0.0.200
2.2.4 方法4:awk 1个管道
[[email protected] oldboy]# ifconfig eth0 |awk-F "[: ]+" ‘NR==2{print $4}‘ 10.0.0.200 [[email protected] oldboy]# echo ‘#####[email protected]@@@@@2‘ #####[email protected]@@@@@2 [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]" #####[email protected]@@@@@2 [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]" -o # # # # # @ @ @ @ @ @ [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]" [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]" #####[email protected]@@@@@2 [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]+" #####[email protected]@@@@@2 [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|egrep "[#@]+" -o ##### @@@@@@ [[email protected] oldboy]# echo‘#####[email protected]@@@@@2‘|awk -F "[@#]+" ‘{print $2}‘ 1
2.2.5 方法5:grep 正则
[[email protected] oldboy]# ifconfig eth0 |egrep"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |egrep"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" -o 10.0.0.200 10.0.0.255 255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |egrep"[0-9]+\.{3}[0-9]+" [[email protected] oldboy]# ifconfig eth0 |egrep"[0-9]+\.{3}[0-9]+" [[email protected] oldboy]# ifconfig eth0 |egrep"([0-9]+\.){3}[0-9]+" inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [[email protected] oldboy]# ifconfig eth0 |egrep"([0-9]+\.){3}[0-9]+" -o 10.0.0.200 10.0.0.255 255.255.255.0 [[email protected] tmp]# ifconfig eth0 |egrep"([0-9]+\.?){4}" -o |head -1 192.168.56.130
2.3 取出文件的数字权限 644 或 0644
[[email protected] oldboy]# stat /etc/hosts File:`/etc/hosts‘ Size: 181 Blocks:8 IO Block: 4096 regular file Device: 803h/2051d Inode:260126 Links: 2 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-09-24 00:03:04.019681888 +0800 Modify: 2017-09-10 20:18:34.077204333 +0800 Change: 2017-09-10 20:18:34.096205176 +0800 [[email protected] oldboy]# stat /etc/hosts |awk-F "[(/]" ‘NR==4{print $2 }‘ 0644
总结:
通配符与特殊符号
通配符号-找文件名
{}
*
特殊符号
&&
>>
>
/
$
.
..
~
|
#
!
&&
||
单引号,双引号,不加引号区别
echo‘$LANG $(hostname) {1..5}‘
‘‘ 单引号:所见即所得,吃啥吐啥
"" 双引号:具有特殊含义的符号 linux对特殊符号进行了解析 执行
不加引号:与使用双引号的时候相同,支持通配符
`` 反引号 $():运行里面的命令,把命令显示出来的东西留下了
2.正则根据题目
排除文件空行
取出网卡ip地址 grep awk sed(后向引用)
取出权限 grep awk sed(后向引用)
3.第三关练习题
4.linux权限
permission denied
本文出自 “老潘Linux” 博客,请务必保留此出处http://oldpan.blog.51cto.com/1603893/1976387
以上是关于老男孩Linux运维第41期20171016第六周学习重点课堂记录的主要内容,如果未能解决你的问题,请参考以下文章
老男孩Linux运维第41期20170924开班第五周学习重点课堂记录
老男孩Linux运维第41期20170924开班第五周学习重点课堂记录
老男孩Linux运维第41期20170917开班第四周学习重点课堂记录
老男孩Linux运维第41期20170903开班第二周学习重点记录