1 四剑客
1.1 概述
1.2 find命令基本用法
1.2.1 找出/etc/目录下面以.conf结尾的文件⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf | head -5
/etc/resolv.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
1.2.2 找出/etc/目录下面以.conf结尾的文件文件大小大于10k⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf -size +10k | head -5
/etc/lvm/lvm.conf
/etc/httpd/conf/httpd.conf
/etc/asciidoc/asciidoc.conf
/etc/asciidoc/docbook45.conf
/etc/asciidoc/docbook5.conf
1.2.3 找出/var/log下面以.log结尾的文件并且修改时间大于3天⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -type f -name *.conf -mtime +3 | head -5
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
/etc/dnf/plugins/copr.conf
1.2.4 查找文件或目录的时候不区分大小写⭐⭐⭐⭐⭐
[root@Kylin-V10-sp3 ~/test]# find /etc/ -iname *.conf | head -5
/etc/resolv.conf
/etc/dnf/protected.d/systemd.conf
/etc/dnf/protected.d/sudo.conf
/etc/dnf/protected.d/dnf.conf
/etc/dnf/protected.d/yum.conf
1.2.5 根据深度查找文件
[root@Kylin-V10-sp3 ~/test]# find /etc/ -maxdepth 2 -type f -size +20k -name *.conf -mtime +3 | head -5
/etc/lvm/lvm.conf
/etc/asciidoc/docbook45.conf
/etc/asciidoc/docbook5.conf
/etc/asciidoc/html5.conf
/etc/asciidoc/xhtml11.conf
[root@Kylin-V10-sp3 ~/test]#
1.3 find与其他命令配合
1.3.1 find找出文件后进行删除 ⭐⭐⭐⭐⭐
点击查看代码
# 创建测试环境
[root@Kylin-V10-sp3 ~/test]# pwd
/root/test
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]# ll
total 8
-rw-r--r-- 1 root root 0 Sep 9 08:59 01.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 02.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 03.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 04.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 05.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 06.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 07.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 08.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 09.txt
-rw-r--r-- 1 root root 0 Sep 9 08:59 10.txt
[root@Kylin-V10-sp3 ~/test]#
# 方法01: find与反引号 ⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 ~/test]# rm -f `find /root/test/ -type f -name '*.txt' `
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep 8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]#
# 方法02: find 管道 ⭐ ⭐ ⭐ ⭐ ⭐
'''
| 与|xargs 区别
|传递的是字符串,文字符号
|xargs 传递是参数 命令后面文件,目录
'''
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# find /root/test/ -type f -name '*.txt' | xargs rm -f
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep 8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]#
# 方法03: find选项 -exec
'''
-exec 命令 {} \;
{} 前面find找出的文件内容
\; 结尾标记.
{} + + 先执行前面命令执行完成,结果一次性通exec传递给后面命令.
'''
[root@Kylin-V10-sp3 ~/test]# touch {01..10}.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 0 Sep 9 09:06 01.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 02.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 03.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 04.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 05.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 06.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 07.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 08.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 09.txt
-rw-r--r-- 1 root root 0 Sep 9 09:06 10.txt
-rw-r--r-- 1 root root 1220 Sep 8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# find /root/test/ -type f -name '*.txt' -exec rm -f {} +
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# ll
total 4
-rw-r--r-- 1 root root 1220 Sep 8 07:48 msg.log
[root@Kylin-V10-sp3 ~/test]#
1.3.2 找出/etc/下以.conf结尾的文件与打包压缩/backup/ ⭐⭐⭐⭐⭐
# 方法01:find+反引号⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 /backup]# tar zcvf /backup/`date +%F_%w`.tar.gz `find /etc/ -type f -name '*.conf'`
# 检查压缩包
[root@Kylin-V10-sp3 /backup]# tar tf 2024-09-09_1.tar.gz
# 方法02:find |xargs⭐ ⭐ ⭐ ⭐ ⭐
[root@Kylin-V10-sp3 /backup]# find /etc/ -type f -name '*.conf' | xargs tar zcvf /backup/`date +%F_%w`.tar.gz
[root@Kylin-V10-sp3 /backup]# ll
total 244
-rw-r--r-- 1 root root 151626 Sep 9 09:28 2024-09-09_1.tar.gz
-rw-r--r-- 1 root root 92160 Sep 8 06:45 etc_conf.tar.gz
# 方法03: find -exec
'''
有坑,用 {} \; 发现打包压缩后只有1个文件.
find 与-exe执行流程
find找出1个文件 exec执行1次命令
{} + + 先执行前面命令执行完成,结果一次性通exec传递给后面命令.
'''
[root@Kylin-V10-sp3 /backup]# find /etc/ -type f -name '*.conf' -exec tar zcvf /backup/`date +%F_%w`.tar.gz {} +
1.3.3 find命令与cp/mv
点击查看代码
'''
背景:
开启yum缓存
vim /etc/yum.conf
keepcache=1 #开启缓存软件包功能
缓存/var/cache/yum目录 以.rpm结尾.
find与|xargs传参
cp /backup/rpms/ 参数 ... . .. . . .
cp 文件 目录 目标(目录)
cp -t 目标(目录) 文件 目录
'''
#需求:把/var/cache/yum目录 以.rpm结尾,复制/移动到指定的目录/backup/rpms/
方法01: find+反引号
[root@Kylin-V10-sp3 /backup/rpms]# cp `find /var/cache/yum/ -type f -name *.rpm` /backup/rpms/
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root 140704 Sep 9 09:41 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 60128 Sep 9 09:41 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 271828 Sep 9 09:41 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 56364 Sep 9 09:41 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root 83736 Sep 9 09:41 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root 505932 Sep 9 09:41 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 8248 Sep 9 09:41 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 9288 Sep 9 09:41 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 17808 Sep 9 09:41 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 27480 Sep 9 09:41 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 16504 Sep 9 09:41 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 49608 Sep 9 09:41 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 72068 Sep 9 09:41 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep 9 09:41 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 64340 Sep 9 09:41 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root 52192 Sep 9 09:41 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep 9 09:41 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]#
方法02:find+|xargs
[root@Kylin-V10-sp3 /backup/rpms]# find /var/cache/yum/ -type f -name *.rpm | xargs cp -t /backup/rpms/
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root 140704 Sep 9 09:42 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 60128 Sep 9 09:42 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 271828 Sep 9 09:42 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 56364 Sep 9 09:42 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root 83736 Sep 9 09:42 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root 505932 Sep 9 09:42 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 8248 Sep 9 09:42 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 9288 Sep 9 09:42 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 17808 Sep 9 09:42 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 27480 Sep 9 09:42 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 16504 Sep 9 09:42 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 49608 Sep 9 09:42 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 72068 Sep 9 09:42 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep 9 09:42 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 64340 Sep 9 09:42 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root 52192 Sep 9 09:42 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep 9 09:42 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]#
方法03:find+ exec
[root@Kylin-V10-sp3 /backup/rpms]# rm -f *.rpm
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 0
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# find /var/cache/yum/ -type f -name *.rpm -exec cp -t /backup/rpms/ {} +
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# ll
total 9068
-rw-r--r-- 1 root root 140704 Sep 9 09:44 createrepo_c-0.16.0-3.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 60128 Sep 9 09:44 drpm-0.5.0-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 271828 Sep 9 09:44 gperftools-libs-2.8-1.ky10.x86_64.rpm
-rw-r--r-- 1 root root 56364 Sep 9 09:44 libunwind-1.3.1-3.ky10.x86_64.rpm
-rw-r--r-- 1 root root 83736 Sep 9 09:44 lrzsz-0.12.20-46.ky10.x86_64.rpm
-rw-r--r-- 1 root root 505932 Sep 9 09:44 nginx-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 8248 Sep 9 09:44 nginx-all-modules-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 9288 Sep 9 09:44 nginx-filesystem-1.21.5-2.p02.ky10.noarch.rpm
-rw-r--r-- 1 root root 17808 Sep 9 09:44 nginx-mod-http-image-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 27480 Sep 9 09:44 nginx-mod-http-perl-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 16504 Sep 9 09:44 nginx-mod-http-xslt-filter-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 49608 Sep 9 09:44 nginx-mod-mail-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 72068 Sep 9 09:44 nginx-mod-stream-1.21.5-2.p02.ky10.x86_64.rpm
-rw-r--r-- 1 root root 6146064 Sep 9 09:44 nmap-7.92-1.p01.ky10.x86_64.rpm
-rw-r--r-- 1 root root 64340 Sep 9 09:44 telnet-0.17-77.ky10.x86_64.rpm
-rw-r--r-- 1 root root 52192 Sep 9 09:44 tree-1.8.0-2.ky10.x86_64.rpm
-rw-r--r-- 1 root root 1664616 Sep 9 09:44 vim-enhanced-9.0-19.p03.ky10.x86_64.rpm
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]#
1.4 特殊符号之引号系列
四剑客命令单独记忆.
引号 |
特点 |
应用场景 |
单引号 |
所见即所得,单引号里面的内容会原封不动输 出,shell(bash)不会解析与运行. |
三剑客, 脚本. |
双引号 |
与单引号类似,特殊符号会被解析运行 $ , `` ,不会解析{} (通配符匹配文件名) |
三剑客, 脚本. |
不加引号 |
与双引号类似,解析{} |
大部分命令 |
反引号 |
优先执行,先运行引号里面的命令 |
|
[root@Kylin-V10-sp3 /backup/rpms]# echo 'hello $LANG `hostname` {01..10}'
hello $LANG `hostname` {01..10}
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# echo "hello $LANG `hostname` {01..10}"
hello en_US.UTF-8 Kylin-V10-sp3 {01..10}
[root@Kylin-V10-sp3 /backup/rpms]#
[root@Kylin-V10-sp3 /backup/rpms]# echo hello $LANG `hostname` {01..10}
hello en_US.UTF-8 Kylin-V10-sp3 01 02 03 04 05 06 07 08 09 10
[root@Kylin-V10-sp3 /backup/rpms]#
1.5 特殊符号之重定向符号系列
重定向符号 |
说明 |
应用场景 |
> 或 1> ⭐ ⭐ ⭐ ⭐ ⭐ |
标准输出重定向(正确信息),先清空,然后写入 |
|
>> 或 1>> ⭐ ⭐ ⭐ ⭐ ⭐ |
标准输出追加重定向(正确信息),追加到文件末尾 |
|
2> |
标准错误输出重定向(错误信息),先清空, 然后写入 |
|
2>> |
标准错误追加输出重定向(错误信息),追 加到文件末尾 |
|
>>1.txt 2>&1 &>>1.txt⭐ ⭐ ⭐ ⭐ ⭐ |
标准错误信息和正确信息都写入到指定文件 |
脚本,定时任务 |
< 或 0< |
输入重定向 |
tr,xargs |
<< 或 0<< |
追加输入重定向 |
cat |
点击查看代码
# 获取错误信息
[root@Kylin-V10-sp3 ~/test]# cho 2>>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# 123 2>>1.txt
[root@Kylin-V10-sp3 ~/test]# hello 2>>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# cat 1.txt
-bash: cho: command not found
-bash: 123: command not found
-bash: hello: command not found
[root@Kylin-V10-sp3 ~/test]#
# 同时记录正确和错误信息
#原始写法
[root@Kylin-V10-sp3 /backup/rpms]# cd /root/test/
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# cho 2>>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# 123 2>>1.txt
[root@Kylin-V10-sp3 ~/test]# hello 2>>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# cat 1.txt
-bash: cho: command not found
-bash: 123: command not found
-bash: hello: command not found
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# echo one two tree 2>>1.txt >>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# cat 1.txt
-bash: cho: command not found
-bash: 123: command not found
-bash: hello: command not found
one two tree
#精简写法(推荐记忆)
[root@Kylin-V10-sp3 ~/test]# 997 >>1.txt 2>&1
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# cat 1.txt
-bash: cho: command not found
-bash: 123: command not found
-bash: hello: command not found
one two tree
-bash: 997: command not found
#极简写法(不是所有系统都支持)
[root@Kylin-V10-sp3 ~/test]# 955 &>>1.txt
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]#
# tr命令简易的替换(sed命令阉割版)
[root@Kylin-V10-sp3 ~/test]# echo 123 |tr '123' 'abc'
abc
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# echo 112233 |tr '123' 'abc'
aabbcc
[root@Kylin-V10-sp3 ~/test]# tr 'a-z' 'A-Z' < /etc/passwd
# <<与cat命令实现文件写入多行
[root@Kylin-V10-sp3 ~/test]# cat <<EOF >2.txt
> I
> love
> linux.
> EOF
[root@Kylin-V10-sp3 ~/test]# cat 2.txt
I
love
linux.
[root@Kylin-V10-sp3 ~/test]#
# 使用cat命令替代部分echo命令功能(脚本中)
小结:
重定向符号 |
说明 |
应用场景 |
> 或 1> ⭐ ⭐ ⭐ ⭐ ⭐ |
标准输出重定向(正确信息),先清空,然后写入 |
大部分,日常使用最多 |
>> 或 1>> ⭐ ⭐ ⭐ ⭐ ⭐ |
标准输出追加重定向(正确信息),追加到文件末尾 |
大部分,日常使用最多 |
2> |
标准错误输出重定向(错误信息),先清空, 然后写入 |
较少单独使用. |
2>> |
标准错误追加输出重定向(错误信息),追 加到文件末尾 |
较少单独使用. |
>>1.txt 2>&1 &>>1.txt⭐ ⭐ ⭐ ⭐ ⭐ |
标准错误信息和正确信息都写入到指定文件 |
未来书写脚本,定时任务使用 |
< 或 0< |
输入重定向 |
tr,xargs |
<< 或 0<< |
追加输入重定向 |
cat写入多行内容到文件,输出多个变量内容 |
2 正则
2.1 为何使用正则
使用三剑客进行文件的处理(过滤),软件服务nginx.
python,golang,php,java语言也支持正则表达式.
2.2 正则分类
分类 |
符号 |
基础正则 |
^ $ ^$ . * .* [] [^] |
扩展正则 |
+ () {} ? |
特殊正则 |
待补充 |
2.3 基础正则
2.3.1 环境准备
cat >re.txt<<EOF
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
EOF
2.3.2 使用
点击查看代码
'''
grep -o 显示正则匹配的内容
正则表达式有贪婪性: 表示连续出现或所有的时候,尽可能多匹配
grep -v vs [^]
-v 以行为单位.
[^] 字符
转义字符,脱掉马甲打回原形.去掉符号特殊含义
'''
# ^my开头行
[root@Kylin-V10-sp3 ~/test]# grep ^my re.txt
my blog is https://www.cnblogs.com/daofaziran
my qq is 12345600000
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 以com 结尾的行
[root@Kylin-V10-sp3 ~/test]# grep 'com $' re.txt
our size is http://blog..com
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]#
# ^$ 空行,这行中没有任何字符,包含空格 查看配置文件的时候排除空行.
[root@Kylin-V10-sp3 ~/test]# grep -nv '^$' re.txt
1:I am a teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:my blog is https://www.cnblogs.com/daofaziran
6:our size is http://blog..com
8:my qq is 12345600000
9:not 65400321000.
11:my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 任意一个字符刚开始学正则,较少单独使用.
[root@Kylin-V10-sp3 ~/test]# grep -n '.' re.txt
1:I am a teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:my blog is https://www.cnblogs.com/daofaziran
6:our size is http://blog..com
8:my qq is 12345600000
9:not 65400321000.
11:my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 连续出现,重复 a* 前一个字符连续出现0次或0次以上 与*一起使用
[root@Kylin-V10-sp3 ~/test]# grep '0*' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
#.* 所有
[root@Kylin-V10-sp3 ~/test]# grep '^.*http' re.txt
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
# 以my开头的行以空格结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '^my.* $' re.txt
my blog is https://www.cnblogs.com/daofaziran
[root@Kylin-V10-sp3 ~/test]#
# [] [abc] 一个整体,每次匹配1个字符,匹配a或b或c
# 常用格式
[root@Kylin-V10-sp3 ~/test]# grep '[a-z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[A-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[0-9]' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]#
# 大小写
[root@Kylin-V10-sp3 ~/test]# grep '[a-z,A-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# grep '[a-zA-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]# grep '[a-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 大小写加上数字
[root@Kylin-V10-sp3 ~/test]# grep '[a-zA-Z0-9]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep '[0-Z]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 过滤出re.txt文件中以m或n开头并且以空格 点叹号结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '^[mn].*[ .!$]' re.txt
my blog is https://www.cnblogs.com/daofaziran
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# [^] [^abc] 一个整体,每次匹配1个字符,非a或非b或非c
[root@Kylin-V10-sp3 ~/test]# grep '[^abc]' re.txt
I am a teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is https://www.cnblogs.com/daofaziran
our size is http://blog..com
my qq is 12345600000
not 65400321000.
my god ,i am not gril,but god!
[root@Kylin-V10-sp3 ~/test]#
# 过滤出文件中以.结尾的行
[root@Kylin-V10-sp3 ~/test]# grep '\.$' re.txt
I teach linux.
not 65400321000.
正则小结
基础正则 |
说明 |
^ |
以...开头的行 |
$ |
以...结尾的行 |
^$ |
匹配空行 |
. |
任意一个字符 |
* |
连续出现0次或0次以上 |
.* |
所有 |
[] |
[abc] [mn] [a-z] [A-Z] [A-Z] [0-9] [a-zA-Z0-9] |
[^] |
取反 |
2.4 扩展正则
支持命令不同 |
说明 |
基础正则 |
grep/sed/awk |
扩展正则 |
grep -E/egrep/sed -r/awk |
2.4.1 |或者
[root@Kylin-V10-sp3 ~/test]# egrep 'root|xk' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# grep -E 'root|xk' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@Kylin-V10-sp3 ~/test]#
2.4.2 + 连续出现,重复前一个字符连续出现1次或1次以上
点击查看代码
# 单独字符使用
[root@Kylin-V10-sp3 ~/test]# egrep '0+' re.txt
my qq is 12345600000
not 65400321000.
# +与[]一起使用
[root@Kylin-V10-sp3 ~/test]# egrep '[0-9]+' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]# egrep -o '[0-9]+' re.txt
12345600000
65400321000
# 统计文件中的单词出现次数.
[root@Kylin-V10-sp3 ~/test]# egrep -o '[a-z]+' re.txt | sort | uniq -c
1 a
2 am
1 and
1 badminton
2 ball
1 billiard
2 blog
1 but
1 chess
1 chinese
1 cnblogs
2 com
1 daofaziran
2 god
1 gril
1 http
1 https
1 i
3 is
1 like
1 linux
3 my
2 not
1 our
1 qq
1 size
1 teach
1 teacher
1 www
[root@Kylin-V10-sp3 ~/test]#
# 统计文件中的字符出现的次数
[root@Kylin-V10-sp3 ~/test]# egrep -o '.' re.txt | sort | uniq -c
33
3 !
3 ,
6 .
5 /
2 :
10 0
2 1
2 2
2 3
2 4
2 5
2 6
13 a
8 b
7 c
6 d
8 e
1 f
6 g
6 h
13 i
3 I
1 k
12 l
8 m
9 n
12 o
2 p
2 q
5 r
9 s
10 t
3 u
3 w
1 x
3 y
2 z
[root@Kylin-V10-sp3 ~/test]# egrep -o '[^ ]' re.txt | sort | uniq -c |sort -rn
13 i
13 a
12 o
12 l
10 t
10 0
9 s
9 n
8 m
8 e
8 b
7 c
6 h
6 g
6 d
6 .
5 r
5 /
3 y
3 w
3 u
3 I
3 ,
3 !
2 z
2 q
2 p
2 6
2 5
2 4
2 3
2 2
2 1
2 :
1 x
1 k
1 f
2.4.3 {} 连续出现,重复 a{n,m} 前一个字符a,连续出现至少n次,最多m次.
'''
a{n,m}
a{m}
'''
# 表示范围
[root@Kylin-V10-sp3 ~/test]# egrep '0{1,3}' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# egrep -o '0{1,3}' re.txt
000
00
00
000
[root@Kylin-V10-sp3 ~/test]#
# 精确的匹配
[root@Kylin-V10-sp3 ~/test]# egrep -o '0{3}' re.txt
000
000
[root@Kylin-V10-sp3 ~/test]# egrep '0{3}' re.txt
my qq is 12345600000
not 65400321000.
[root@Kylin-V10-sp3 ~/test]#
2.4.4 ()被括起来的内容相当于是一个整体 ,分组与反向引用 一般用于sed命令中
[root@Kylin-V10-sp3 ~/test]# export LANG=en_US.UTF-8
[root@Kylin-V10-sp3 ~/test]# lscpu | egrep 'L(1d|1i|2|3) cache'
L1d cache: 32 KiB
L1i cache: 32 KiB
L2 cache: 256 KiB
L3 cache: 8 MiB
[root@Kylin-V10-sp3 ~/test]#
# 过滤不同类型的图片.jpg png jpeg
egrep '\.(jpg|png|jpeg)$' xxx.log
2.4.5 ? 连续出现,重复 出现1次或出现0次
[root@Kylin-V10-sp3 ~/test]# cat >01.txt<<EOF
> good
> gooood
> god
> gd
> EOF
[root@Kylin-V10-sp3 ~/test]#
[root@Kylin-V10-sp3 ~/test]# egrep 'go?d' 01.txt
god
gd
[root@Kylin-V10-sp3 ~/test]#
2.4.6 扩展正则小结
符号 |
|
| |
或者 |
+ |
连续出现1次或1次以上 |
{} |
连续出现范围 a{n,m} a |
() |
整体, 分组引用(sed) |
? |
0次或1次 |
2.5 正则总结
# 连续出现:
? 0-1
{} 范围
* 0-xxxx
+ 1-xxxx
# 或者:
[]
[^]
|
# 位置:
^
$
^$
# 其他
.
.*
()