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}
#指定每次变化多少
[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
1.4 正则表达式
1.4.1 什么是正则
又称规则表达式。(英语:RegularExpression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。
创建试验环境:
cat > test.txt <<EOFoldboy 230xxxxxxxxxxxxxxxxxxxoldboy01oldboy43.txtoldboy-lidao.txtoldboybabyoldbabayEOF
找出文件中包含数字的行
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 [root@oldboyedu41-nb 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 准备测试环境
[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!
1.4.6 基础正则
1.4.6.1 ^ 表示以....开头的行
[root@oldboyedu41-nb oldboy]# grep"^m" oldboy.txtmy 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可以在文件每一行最后加上结尾符$
[root@oldboyedu41-nb oldboy]# cat -Aoldboy.txtI 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.txtmy blog is http://oldboy.blog.51cto.com
1.4.6.3 空行 ^$ 这一行中没有任何符号
[root@oldboyedu41-nb oldboy]# grep -n"^$" oldboy.txt3: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.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000448.not 4900000448.my god ,i am not oldbey,but OLDBOY!
1.4.6.4 .(点) 匹配任意一个字符但不匹配空行
[root@oldboyedu41-nb oldboy]# #grep -o "." oldboy.txt[root@oldboyedu41-nb oldboy]# #-o grep命令每次 匹配到了什么[root@oldboyedu41-nb oldboy]# grep -o "." oldboy.txt
1.4.6.5 \ 转义字符 脱掉马甲 打回原形
#找出文件中oldboy.txt 以.结尾的行
[root@oldboyedu41-nb oldboy]# grep"\.$" oldboy.txtI teach linux.my qq num is 49000448.not 4900000448.
\n 回车换行
#^ $ ^$ .
#grep -o 显示grep命令每次找到的内容
1.4.6.6 * 前一个字符连续出现0次或1次以上 >=0
[root@oldboyedu41-nb oldboy]# grep"0" oldboy.txtmy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# grep -o"0" oldboy.txt00000000[root@oldboyedu41-nb oldboy]# grep -on"0" oldboy.txt7:07:07:09:09:09:09:09:0
[root@oldboyedu41-nb oldboy]# grep"0*" oldboy.txtI am oldboy teacher!I teach linux. I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000448. not 4900000448.my god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# grep -o"0*" oldboy.txt00000000
连续出现
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 .* 所有任何东西
[root@oldboyedu41-nb oldboy]# grep -n"^.*o" oldboy.txt1:I am oldboy teacher!4:I like badminton ball ,billiard ball andchinese chess!5:my blog is http://oldboy.blog.51cto.com6:our site is http://www.etiantian.org9: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.txtmy blog is http://oldboy.blog.51cto.commy 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.txtmy 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 结尾的行
[root@oldboyedu41-nb oldboy]# grep"^[mno]" oldboy.txtmy blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq num is 49000448.not 4900000448.my god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# grep "^[mno].*[mg]$"oldboy.txtmy blog is http://oldboy.blog.51cto.comour 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啦?
1.4.6.9 [^] [^abc] 表示一个整体相当于是一个符号 表示包含a或者b或者c的行
第一个位置 排除
[root@oldboyedu41-nb oldboy]# grep"[xlc]" oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# grep"[^xlc]" oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy 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.4.6.10 基础正则表达式小结:
(1) .^ $ ^$ .* [abc]
(2) grep grep -o -o参数把找到的东西显示出来
1.4.7 扩展正则ERE
1.4.7.1 + 前一个字符连续出现1次或多次 >=1
[root@oldboyedu41-nb oldboy]# grep"0+" oldboy.txt[root@oldboyedu41-nb oldboy]# egrep"0+" oldboy.txtmy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# #egrep ===grep -E [root@oldboyedu41-nb oldboy]# egrep"0+" oldboy.txt -o00000000[root@oldboyedu41-nb oldboy]# #取出文件中连续出现的小写字母?[root@oldboyedu41-nb oldboy]# egrep'[a-z]+' oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy 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.txtmy blog is http://oldboy.blog.51cto.commy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep"[0-9]" oldboy.txt -o51490004484900000448[root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# egrep"[0-9]+" oldboy.txt my blog is http://oldboy.blog.51cto.commy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep"[0-9]+" oldboy.txt -o51490004484900000448[root@oldboyedu41-nb oldboy]# #+ 连续出现的东西 一次性取出来 变为一个整体
1.4.7.2 | 表示或者
[root@oldboyedu41-nb oldboy]# egrep 'a|b|c'oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# egrep"[abc]" oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# egrep"oldboy|oldbey" oldboy.txtI am oldboy teacher!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY!
1.4.7.3 () 的作用:1括号里面的内容相当于是一个整体,先算括号里面的内容;2反向引用(sed)
[root@oldboyedu41-nb oldboy]# egrep"oldboy|oldbey" oldboy.txtI am oldboy teacher!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# egrep"oldbo|ey" oldboy.txtI am oldboy teacher!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# egrep"oldb(o|e)y" oldboy.txtI am oldboy teacher!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY!
2、反向引用(sed) 先保护再使用
[root@oldboyedu41-nb oldboy]# echo 123456123456[root@oldboyedu41-nb oldboy]# echo 123456123456[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>
1.4.7.4 a{n,m} 0{n,m} 前一个字符连续出现了最少n次,最多出现了m次
* >=0
+ >=1
[root@oldboyedu41-nb oldboy]# egrep'0{1,3}' oldboy.txtmy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep'0{1,3}' oldboy.txt -o00000000[root@oldboyedu41-nb oldboy]# egrep'0{1,3}' oldboy.txt -on7:0009:0009: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 -o0000000[root@oldboyedu41-nb oldboy]# egrep'0{2,4}' oldboy.txt -on7:0009:0000 #找出文件中连续出现5次到6次的字母[root@oldboyedu41-nb oldboy]# egrep '[a-z]{5,6}' oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# egrep -w '[a-z]{5,6}' oldboy.txtI am oldboy teacher!I teach linux.I like badminton ball ,billiard ball andchinese chess!my blog is http://oldboy.blog.51cto.commy god ,i am not oldbey,but OLDBOY![root@oldboyedu41-nb oldboy]# [root@oldboyedu41-nb oldboy]# egrep -wo '[a-z]{5,6}' oldboy.txtoldboyteachlinuxchessoldboyoldbey[root@oldboyedu41-nb oldboy]# egrep -won '[a-z]{5,6}' oldboy.txt1:oldboy2:teach2:linux4:chess5:oldboy10:oldbey[root@oldboyedu41-nb oldboy]# egrep -woni '[a-z]{5,6}' oldboy.txt1:oldboy2:teach2:linux4:chess5:oldboy10:oldbey10:OLDBOY #-w grep按照单词进行匹配(全字符精确匹配/完全匹配) grep oldboy test.txt oldboy oldboybaby testoldboyhelloldboy 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.txtmy blog is http://oldboy.blog.51cto.commy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep"[0-9]{5,}" oldboy.txtmy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep"[0-9]{5,}" oldboy.txt -o490004484900000448
#-i 不区分大小写 aA
#-w 按照单词进行匹配(全字符精确匹配/完全匹配)
#-n 显示行号
#-o 显示grep执行过程 grep正则每次匹配到的内容显示出来
[root@oldboyedu41-nb oldboy]# egrep '[0-9]{5}' oldboy.txtmy qq num is 49000448.not 4900000448.[root@oldboyedu41-nb oldboy]# egrep '[0-9]{5}' oldboy.txt -o490004900000448
{}小结:
1. a{n,m} 前一个字符连续出现了最少n次,最多出现了m次 >=n <=m
2. a{n,} 前一个字符连续出现了最少n次 >=n
3. a{n} 前一个字符连续出现了n次 ==n
1.4.7.5 ? 前一个字符连续出现了0次或1次
[root@oldboyedu41-nb oldboy]# egrep"go?d" a.log gd god [root@oldboyedu41-nb oldboy]# egrep"go*d" a.log goodgd godgoood
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<<EOFwelcome 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个管道
[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 " " -f12addr: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
2.2.2 方法2:awk 2个管道
[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
2.2.3 方法3:awk 1个管道
[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
2.2.4 方法4:awk 1个管道
[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
2.2.5 方法5:grep 正则
[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]+" -o10.0.0.20010.0.0.255255.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]+" -o10.0.0.20010.0.0.255255.255.255.0 [root@oldboyedu41-1 tmp]# ifconfig eth0 |egrep"([0-9]+\.?){4}" -o |head -1192.168.56.130
2.3 取出文件的数字权限 644 或 0644
[root@oldboyedu41-nb oldboy]# stat /etc/hosts File:`/etc/hosts' Size: 181 Blocks:8 IO Block: 4096 regular fileDevice: 803h/2051d Inode:260126 Links: 2Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Access: 2017-09-24 00:03:04.019681888 +0800Modify: 2017-09-10 20:18:34.077204333 +0800Change: 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