当前位置:网站首页>grep与正则的搭配使用
grep与正则的搭配使用
2022-07-24 05:23:00 【笑 瘾】
正则表达式
一、什么是正则
使用一些特殊符号+字母和数字按照某个规则组合成一个公式用来表示某个意思这就叫正则表达式
二、正则用在哪些地方
查找的场景特别适合使用正则
vim 、grep、sed、awk等
三、为什么需要使用正则
四、正则和shell里面的区别
1、shell里面:
shell里的通配符: [] {} ^ $ !
shell里面的?代表通配一个任意字符
shell里面的*代表任意个任意字符
2、正则里面:
元字符:有特殊作用的字符,能描述其他字符的字符
元字符:
^ 表示以什么开头
$ 表示以什么结尾
? 表示前面的字符串出现0或者1次
+表示前面的字符串出现1次以上
*表示前面的字符串出现0次或者任意次
[[email protected] lianxi]# rm -rf wangyutao*
[[email protected] lianxi]# echo wangyuuuutaoooo|egrep "wangyu*"
wangyuuuutaoooo
[[email protected] lianxi]# echo wangyuuuuuuuuuuuuuuuuuuuuuuuuuuuutaoooo|egrep "wangyu*"
wangyuuuuuuuuuuuuuuuuuuuuuuuuuuuutaoooo
[[email protected] lianxi]# echo wangytaoooo|egrep "wangyu*"
wangytaoooo
五、基本正则和扩展正则的区别
1、egrep = grep -E
[[email protected] lianxi]# cat name.txt |grep -E -i "wenkeke$|fangfang$"
wenke wenkeke
wanglianfang fangfang
wenke wenkeke
wanglianfang fangfang
[[email protected] lianxi]# cat name.txt |egrep -i "wenkeke$|fangfang$"
wenke wenkeke
wanglianfang fangfang
wenke wenkeke
wanglianfang fangfang
2、基本正则:
元字符: * . ^ $
[[email protected] lianxi]# cat name.txt |egrep "^xiaomi"
xiaomi huawei oppo
xiaomi huawei oppo
[[email protected] lianxi]# cat name.txt |egrep "^xiaomi|lili$"
xiaomi huawei oppo
liyili lili
xiaomi huawei oppo
liyili lili
# 匹配空行:
[[email protected] lianxi]# cat name.txt |egrep "^$"
#显示行号:
[[email protected] lianxi]# cat name.txt |egrep -n "shenjiemi"
16:shenjiemi
17:shenjiemi lijunlin
[[email protected] lianxi]# cat name.txt |egrep -n "^shenjiemi$" shenjiemi单独是一行
16:shenjiemi
3、扩展正则:
元字符: | + ? {}
[[email protected] lianxi]# cat name.txt
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
wenke wenkeke
wanglianfang fangfang
liyili lili
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
wenke wenkeke
wanglianfang fangfang
liyili lili
zhaojunjie
shenjiemi
shenjiemi lijunlin
1
2
3
4
shenjiedami shenxiaomi
扩展正则的使用
[[email protected] lianxi]# echo "shenjiedami shenxiaomi" >>name.txt ^C
[[email protected] lianxi]# cat name.txt | egrep "shen...."
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[[email protected] lianxi]# cat name.txt | egrep "shen.*" 表示shen字符串后面出现任意个任意字符
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[[email protected] lianxi]# cat name.txt | egrep "shen.{4}"
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
[[email protected] lianxi]# cat name.txt | egrep "shen.{4,6}" 表示shen字符串后面出现4到6个任意字符
shenjiemi
shenjiemi lijunlin
shenjiedami shenxiaomi
4、区别:
扩展正则支持更加多的元字符,能够表示更加复杂的意思,功能更加强大
5、[abc] 取中括号里的一个字符,可以是a也可以是b,也可以是c
等同于"a|b|c"
6、单词的定界符号
< 和 \b 表示单词以什么开头
> 和 \b 表示单词以什么结尾
\<san 单词以san开头
\<sanchuang\> 等同于 \bsanchuang\b
\<sanchuang 等同于\bsanchuang
sanchuang\> 等同于 sanchuang\b
7、查找文本里单词的长度是13个字符的字符串:
grep “\b[a-Z]{13}”
grep命令
一、grep的选项:
grep选项
-i 不区分大小写 -i, --ignore-case
-o 只是显示匹配的内容 only-match
-n 显示行号 line-number
-v invert-match 取反
-A after 在什么之后
-B before 在什么以前
-C center(中心) context上下文
[[email protected] lianxi]# grep -o -i "xiaomi" name.txt
xiaomi
XIAOMI
xiaomi
[[email protected] lianxi]# grep -o -i "xiaomi" name.txt | wc -l
3
[[email protected] lianxi]#
[email protected] lianxi]# cat name.txt |grep -B2 "lizhuofu"
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
--
liyili lili
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
[[email protected] lianxi]# cat name.txt |grep -A2 "lizhuofu"
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
--
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
[[email protected] lianxi]# cat name.txt |grep -A 2 "lizhuofu"
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
--
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
[[email protected] lianxi]# cat name.txt |grep -C 2 "lizhuofu"
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
--
liyili lili
zhengyang ZHENGYANG
LIZHUOFU lizhuofu123 12345lizhuofu
xiaomi huawei oppo
XIAOMI xiaomi12234+56 [email protected]
[[email protected] lianxi]#
[[email protected] lianxi]# grep -r "xiaomi" /lianxi
/lianxi/name.txt:xiaomi huawei oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56 [email protected]
/lianxi/name.txt:xiaomi huawei oppo
/lianxi/name.txt:XIAOMI xiaomi12234+56 [email protected]
sort和uniq命令
一、sort是一个排序命令
默认情况下根据一行里的第1个字母的ASCII码值进行比较,升序排列
如果第1个字符一样,再比较第2个,以此类推
1、选项:
-n 进行数值的比较,默认升序
-k 指定列排序
-r 排序是降序
-t, --field-separator=SEP 指定分隔符
field 字段,列
2、sort的分隔符是空白(空格和tab),多个空白都算一个分隔符

二、uniq是一个去重命令
默认情况下,只能去除连续的重复行
1.选项:-c 统计重复的次数 count

今日练习
1.进入/lianxi目录,复制/etc/passwd到当前目录下,然后对passwd进行操作
2、查找出当前passwd文件中以ftp或者mail开头的行,在屏幕上输出。
3、查找出当前passwd文件中首行不是以r、m、f开头的行,在屏幕上输出。
4、查找出当前passwd文件中以bash结尾的行。
5、查找出/etc/login.defs文件中的有效行(不显示空行和注释行)。
6、查找出/var/log/messages文档中有16个字母的单词
7、查找出来/etc/passwd文件里用户名包含liu同时使用bash的用户
8、查找/etc/ssh/sshd_config 里的有效行
9、查找出/etc/ssh/sshd_config 文件里的包含连续2个数字的行
10、查找出/etc/ssh/sshd_config包含特殊字符的行
11、查找出/etc/ssh/sshd_config不包含数字的行
14、写一个表示下面网址的正则表达式出来。例如:
http://www.baidu.com fjdkfjdkfj
http://www.sina.com fengdeyong
http://www.163.com
http://www.12306.cn 1212121
http://www.qillu.edu sanchuang
rsync://www.github.com/abc
ftp://192.168.0.1
ftp://www.baidu.com 12112
15.时间的正则
时间的正则,表示18/Dec/2021:16:54分钟到18/Dec/2021:16:58分钟
答案



边栏推荐
- Set up a WordPress personal blog locally and launch it through the intranet (22)
- Summary of common working methods (7S, SWOT analysis, PDCA cycle, smart principle, 6w2h, time management, WBS, 28 principles)
- Wasm vs EVM, Boca's choice predicts the future of the public chain
- [award issuance] the results of the first essay solicitation activity in oneos zone were announced
- [226] instructions for Wireshark parameters
- Dameng database_ Small matters needing attention during use
- jz47 礼物的最大价值(动态规划思路)
- Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]
- Luckyframeweb testing platform (a full latitude free open source testing platform that supports interface automation, Web UI automation, APP automation, and distributed testing)
- Do not rent servers, build your own personal business website (1)
猜你喜欢

Leetcode sword finger offer jz25 merges two sorted linked lists

【214】什么是自动化框架

Flink production environment configuration recommendations

Polkadot | interprets how liberty plan, which subverts traditional social media, will be launched in Poka

【218】CS架构和BS架构以及数据放在服务端和客户端的利与弊?

不租服务器,自建个人商业网站(1)

Tensorflow GPU installation -- 056

leetcode剑指offer JZ23:链表中环的入口节点

进程和计划任务管理

第二周作业
随机推荐
迭代器与生成器
IP笔记(9)
IA课总结(1)
Leetcode refers to the duplicate number in the offer jz3 array
ip作业(1)
Data set and pre training model
Dameng database_ Dmfldr tool instructions
NTP error: no server suitable for synchronization found
leetcode剑指offer jz5 替换空格字符串
Leetcode sword finger offer JZ9 dual stack implementation queue
[301] grotesque behavior - predictable irrationality
[218] what are the advantages and disadvantages of CS architecture and BS architecture and data on the server and client?
微信TBS在线安装内核失败的解决方法
Flink state use
Leetcode sword finger offer jz42 maximum sum of continuous subarrays
Maximum value of jz47 gifts (dynamic planning ideas)
sed命令
Understanding of Flink parallelism
Wasm vs EVM, Boca's choice predicts the future of the public chain
Public access intranet IIS website server [no public IP required]