Week6课堂知识点总结 |
作者:Old_Pan 归档:学习笔记 2017/10/16
|
目 录
特殊符号 | 作用 |
单引号‘‘ | 对括号中的内容不作任何解析,所见即所得,吃啥吐啥 |
双引号"" | 和单引号类似,但是特殊符号会被解析 运行 $ $() `` ! |
反引号`` | === $() 先运行里面的命令,然后把显示到屏幕上的内容留下.. |
# | 表示注释以及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.临时取消别名 |
通配符作用:用来匹配文件名
* 所有、任何东西
找出以 *.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*
#生成序列
echo {a..z}
echo {1..10}
echo {01..10}
echo {001..10}
echo {01..10}
echo {01..100}
#指定每次变化多少
[root@oldboyedu41-1 tmp]# echo {01..10..2} 01 03 05 07 09 [root@oldboyedu41-1 tmp]# echo {a..z..2} a c e g i k m o q s u w y [root@oldboyedu41-1 tmp]# echo stu{01,05,10} stu01 stu05 stu10
#通过{}对某个文件进行备份
[root@oldboyedu41-nb oldboy]# echo A{B,C} AB AC [root@oldboyedu41-nb oldboy]# echo A{,C} A AC [root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# echo oldboy.txt{,C} oldboy.txt oldboy.txtC [root@oldboyedu41-nb oldboy]# echo oldboy.txt{,.bak} oldboy.txt oldboy.txt.bak [root@oldboyedu41-nb oldboy]# touch oldboy.txt [root@oldboyedu41-nb oldboy]# cp oldboy.txt{,.bak} [root@oldboyedu41-nb oldboy]# #cp oldboy.txt{,.bak} [root@oldboyedu41-nb oldboy]# #cp oldboy.txt oldboy.txt.bak [root@oldboyedu41-nb oldboy]# echo oldboy.txt{,.bak} oldboy.txt oldboy.txt.bak
又称规则表达式。(英语: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# 正则表达式是高级货色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 [root@oldboyedu41-nb oldboy]# alias grep egrep alias grep=‘grep --color=auto‘ alias egrep=‘egrep --color=auto‘
1# 正则表达式高级货色 查找文件内容 文字符号 grep sed awk 三剑客支持 python java
2# 通配符 查找文件名字大部分linux命令都可以使用 通配符 *.txt *.log
[root@oldboyedu41-nb 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!
[root@oldboyedu41-nb 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!
cat -A可以在文件每一行最后加上结尾符$
[root@oldboyedu41-nb 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!$ [root@oldboyedu41-nb oldboy]# grep"m$" oldboy.txt my blog is http://oldboy.blog.51cto.com
[root@oldboyedu41-nb oldboy]# grep -n"^$" oldboy.txt 3: 8: [root@oldboyedu41-nb 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!
#排除空行
[root@oldboyedu41-nb 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!
[root@oldboyedu41-nb oldboy]# #grep -o "." oldboy.txt [root@oldboyedu41-nb oldboy]# #-o grep命令每次 匹配到了什么 [root@oldboyedu41-nb oldboy]# grep -o "." oldboy.txt
#找出文件中oldboy.txt 以.结尾的行
[root@oldboyedu41-nb oldboy]# grep"\.$" oldboy.txt I teach linux. my qq num is 49000448. not 4900000448.
\n 回车换行
#^ $ ^$ .
#grep -o 显示grep命令每次找到的内容
[root@oldboyedu41-nb oldboy]# grep"0" oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# grep -o"0" oldboy.txt 0 0 0 0 0 0 0 0 [root@oldboyedu41-nb oldboy]# grep -on"0" oldboy.txt 7:0 7:0 7:0 9:0 9:0 9:0 9:0 9:0
[root@oldboyedu41-nb 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! [root@oldboyedu41-nb 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!
[root@oldboyedu41-nb 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! [root@oldboyedu41-nb oldboy]# # #正则中表示连续出现或所有的时候 ----- 贪婪性 [root@oldboyedu41-nb oldboy]# #有多少匹配多少有多少吃多少
#我要找以m开头的行 并且 以m结尾的行
[root@oldboyedu41-nb oldboy]# ##我要找以m开头的行并且 以m结尾的行 [root@oldboyedu41-nb 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! [root@oldboyedu41-nb oldboy]# grep"^m$" oldboy.txt [root@oldboyedu41-nb oldboy]# #grep"^m什么都可以m$"oldboy.txt [root@oldboyedu41-nb oldboy]# grep "^m什么都可以m$" oldboy.txt [root@oldboyedu41-nb oldboy]# grep"^m.*m$" oldboy.txt my blog is http://oldboy.blog.51cto.com
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 结尾的行
[root@oldboyedu41-nb 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! [root@oldboyedu41-nb oldboy]# grep "^[mno].*[mg]$"oldboy.txt my blog is http://oldboy.blog.51cto.com our site is http://www.etiantian.org
[root@oldboyedu-41-01 tmp]# grep"[a-Z]" oldboy.txt
[root@Centos6p9m1 oldboy]# grep"^[m|n|o].*[m|g]$" oldboy.txt [root@Centos6p9m1 oldboy]# grep"^[m,n,o].*[m,g]$" oldboy.txt [root@oldboyedu41-nb oldboy]# #在正则中括号中 特殊符号(正则表达式符号) 一般都没有特殊含义 [root@oldboyedu41-nb oldboy]# #grep"\.$" [root@oldboyedu41-nb oldboy]# #grep"[.]$"
问题:- 表示非特殊符号 在这里怎么是从a到z啦?
第一个位置 排除
[root@oldboyedu41-nb 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! [root@oldboyedu41-nb 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!
[root@Gloria ~]# grep"[^m^n^o].*[m$g$]" oldboy.txt
[^m^n^o]表示 : 排除m或^或n或o
[root@oldboyedu oldboy]# grep"[^m-m$]" oldboy.txt
(1) .^ $ ^$ .* [abc]
(2) grep grep -o -o参数把找到的东西显示出来
[root@oldboyedu41-nb oldboy]# grep"0+" oldboy.txt [root@oldboyedu41-nb oldboy]# egrep"0+" oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# #egrep ===grep -E [root@oldboyedu41-nb oldboy]# egrep"0+" oldboy.txt -o 000 00000 [root@oldboyedu41-nb oldboy]# #取出文件中连续出现的小写字母? [root@oldboyedu41-nb 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 [root@oldboyedu41-nb oldboy]# egrep ‘[a-z]+‘oldboy.txt -o #连续出现 000000 00000 abcd [root@oldboyedu41-nb oldboy]# egrep"[0-9]" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb 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 [root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# egrep"[0-9]+" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# egrep"[0-9]+" oldboy.txt -o 51 49000448 4900000448 [root@oldboyedu41-nb oldboy]# #+ 连续出现的东西 一次性取出来 变为一个整体
[root@oldboyedu41-nb 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! [root@oldboyedu41-nb 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! [root@oldboyedu41-nb oldboy]# [root@oldboyedu41-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@oldboyedu41-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@oldboyedu41-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@oldboyedu41-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、反向引用(sed) 先保护再使用
[root@oldboyedu41-nb oldboy]# echo 123456 123456 [root@oldboyedu41-nb oldboy]# echo 123456 123456 [root@oldboyedu41-nb oldboy]# echo123456|sed ‘s#.*##g‘ [root@oldboyedu41-nb oldboy]# echo 123456|sed‘s#.*#a#g‘ a [root@oldboyedu41-nb oldboy]##<123456> [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.*)#\1#g‘ 123456 [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.*)#<\1>#g‘ <123456>
[root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.*)#\1#g‘ 123456 [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.*)#@@@\1@@@@#g‘ @@@123456@@@@ [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.*)#<\1>#g‘ <123456> [root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#..(.)...#\1#g‘ 3 [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#..(.)...#\2#g‘ sed: -e expression #1, char 15: invalidreference \2 on `s‘ command‘s RHS [root@oldboyedu41-nb oldboy]# echo123456|sed -r ‘s#(.).(.)...#\2#g‘ 3
echo 123456 |sed -r ‘s#(1)(5)#+\1+3\23#g‘
[root@instance-kapw2maf ~]# echo 123456|sed ‘s#(.)(.)(.)#<\2>#g‘ -r <2><5>
* >=0
+ >=1
[root@oldboyedu41-nb oldboy]# egrep‘0{1,3}‘ oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# egrep‘0{1,3}‘ oldboy.txt -o 000 000 00 [root@oldboyedu41-nb oldboy]# egrep‘0{1,3}‘ oldboy.txt -on 7:000 9:000 9:00 [root@oldboyedu41-nb oldboy]# egrep‘0{1,3}‘ oldboy.txt [root@oldboyedu41-nb oldboy]# egrep ‘0{2,4}‘oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# egrep‘0{2,4}‘ oldboy.txt -o 000 0000 [root@oldboyedu41-nb oldboy]# egrep‘0{2,4}‘ oldboy.txt -on 7:000 9:0000 #找出文件中连续出现5次到6次的字母 [root@oldboyedu41-nb 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! [root@oldboyedu41-nb 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! [root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# egrep -wo ‘[a-z]{5,6}‘ oldboy.txt oldboy teach linux chess oldboy oldbey [root@oldboyedu41-nb oldboy]# egrep -won ‘[a-z]{5,6}‘ oldboy.txt 1:oldboy 2:teach 2:linux 4:chess 5:oldboy 10:oldbey [root@oldboyedu41-nb 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
[root@oldboyedu oldboy]# egrep ‘[a-z]+ {1,5}‘ oldboy.txt -o 这样会不会有冲突?
#找出文件中的QQ号码
[root@oldboyedu41-nb oldboy]# ##找出文件中的QQ号码 [root@oldboyedu41-nb oldboy]# egrep"[0-9]" oldboy.txt my blog is http://oldboy.blog.51cto.com my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# egrep"[0-9]{5,}" oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb oldboy]# egrep"[0-9]{5,}" oldboy.txt -o 49000448 4900000448
#-i 不区分大小写 aA
#-w 按照单词进行匹配(全字符精确匹配/完全匹配)
#-n 显示行号
#-o 显示grep执行过程 grep正则每次匹配到的内容显示出来
[root@oldboyedu41-nb oldboy]# egrep ‘[0-9]{5}‘ oldboy.txt my qq num is 49000448. not 4900000448. [root@oldboyedu41-nb 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
[root@oldboyedu41-nb oldboy]# egrep"go?d" a.log gd god [root@oldboyedu41-nb oldboy]# egrep"go*d" a.log good gd god goood
连续出现(重复)
* >=0
+ >=1
? 0 1
{n,m} >=n <=m
其他
. 任意一个字符
[abc] 一个整体 相当于是一个字符
[a-z] [0-9] [A-Z]
[^abc] 排除
| 或者
() 后向引用 反向引用 先保护再使用
^
$
支持基础正则 | 支持基础+扩展正则 |
grep | egrep或者grep -E |
sed | sed -r |
awk |
创建测试环境:
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
[root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘|cut -d " " -f12 addr:10.0.0.200 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘|awk ‘{print $2}‘ addr:10.0.0.200 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘|awk ‘{print $2}‘|awk -F: ‘{print $2}‘ 10.0.0.200
[root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘ inet addr:10.0.0.200 Bcast:10.0.0.255 Mask:255.255.255.0 [root@oldboyedu41-nb 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 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘|awk -F "addr:|Bcast" ‘{print $2}‘ 10.0.0.200 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk‘NR==2‘|awk -F "addr:| Bcast"‘{print $2}‘ 10.0.0.200
[root@oldboyedu41-nb oldboy]# ifconfig eth0|awk‘NR==2{print $2}‘ addr:10.0.0.200 [root@oldboyedu41-nb oldboy]# #sed ‘找谁干啥‘ [root@oldboyedu41-nb oldboy]# #awk ‘找谁{干啥}‘ [root@oldboyedu41-nb oldboy]# #找谁===条件 [root@oldboyedu41-nb oldboy]# ifconfig eth0|awk‘NR==2{print $2}‘ addr:10.0.0.200 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk-F "addr:| Bcast" ‘NR==2{print$2}‘ 10.0.0.200
[root@oldboyedu41-nb oldboy]# ifconfig eth0 |awk-F "[: ]+" ‘NR==2{print $4}‘ 10.0.0.200 [root@oldboyedu41-nb oldboy]# echo ‘#####1@@@@@@2‘ #####1@@@@@@2 [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]" #####1@@@@@@2 [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]" -o # # # # # @ @ @ @ @ @ [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]" [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]" #####1@@@@@@2 [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]+" #####1@@@@@@2 [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|egrep "[#@]+" -o ##### @@@@@@ [root@oldboyedu41-nb oldboy]# echo‘#####1@@@@@@2‘|awk -F "[@#]+" ‘{print $2}‘ 1
[root@oldboyedu41-nb 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 [root@oldboyedu41-nb 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 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |egrep"[0-9]+\.{3}[0-9]+" [root@oldboyedu41-nb oldboy]# ifconfig eth0 |egrep"[0-9]+\.{3}[0-9]+" [root@oldboyedu41-nb 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 [root@oldboyedu41-nb oldboy]# ifconfig eth0 |egrep"([0-9]+\.){3}[0-9]+" -o 10.0.0.200 10.0.0.255 255.255.255.0 [root@oldboyedu41-1 tmp]# ifconfig eth0 |egrep"([0-9]+\.?){4}" -o |head -1 192.168.56.130
[root@oldboyedu41-nb 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 [root@oldboyedu41-nb 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第六周学习重点课堂记录
原文地址:http://oldpan.blog.51cto.com/1603893/1976387