特殊符号和正则表达式

特殊符号

# 的作用
1.在一些配置文件中表示注释
2.在前导符中表示root用户登录当前系统

$ 的作用
1.获取变量内容
echo $PAHT
echo $LANG

! 的作用
1.强制执行

| 的作用
1.管道符,命令拼接

; 的作用
1.命令分隔符,不管前面的命令是否执行成功,都会去执行后面的命令,个数不限制
ls ; pwd ; cd /opt

&& 的作用
1.前面的命令必须执行成功,后面的命令才能继续执行
[root@kylin-xu ~]# echo haha && ll /aaaa && echo jieshu
haha
ls: 无法访问 '/aaaa': 没有那个文件或目录
[root@kylin-xu ~]# echo haha && ll /root  && echo jieshu
haha
总用量 4
-rw------- 1 root root 2726 11月  3 12:29 anaconda-ks.cfg
jieshu

|| 的作用
1.前面的命令必须执行失败,后面的命令才会继续执行
[root@kylin-xu ~]# cd xu || mkdir xu
-bash: cd: xu: 没有那个文件或目录
[root@kylin-xu ~]# ll xu -d
drwxr-xr-x 2 root root 6 11月  6 09:16 xu
[root@kylin-xu ~]# cd xu || mkdir xu && cd xu
-bash: cd: xu: 没有那个文件或目录
[root@kylin-xu xu]# pwd
/root/xu
> (1>): 标准输出正确重定向,只接收正确的
2>:标准错误输出重定向,只接收错误的

[root@kylin-xu ~]# echo haha > test.txt
[root@kylin-xu ~]# cat test.txt 
haha

[root@kylin-xu ~]# ee aaa 2> test.txt
[root@kylin-xu ~]# cat test.txt 
-bash: ee:未找到命令

>>(1>>):标准输出正确追加重定向,只接收正确的
2>>:标准错误输出追加重定向,只接收错误的
[root@kylin-xu ~]# echocccc aaa 2>> test.txt 
[root@kylin-xu ~]# cat test.txt 
-bash: echocccc:未找到命令
[root@kylin-xu ~]# > test.txt 
[root@kylin-xu ~]# cat test.txt 
[root@kylin-xu ~]# echo aaa >> test.txt 
[root@kylin-xu ~]# echo aaa 1>> test.txt 
[root@kylin-xu ~]# cat test.txt 
aaa
aaa


同时接收正确和错误的结果
第一种写法  echo haha 1>> a.txt 2>> a.txt
[root@kylin-xu ~]# echo haha 1>> a.txt 2>> a.txt
[root@kylin-xu ~]# echooooooooooo haha 1>> a.txt 2>> a.txt
[root@kylin-xu ~]# cat a.txt 
haha
-bash: echooooooooooo:未找到命令


第二种写法  echo hehe >> a.txt 2>&1
[root@kylin-xu ~]# echo hehe >> a.txt 2>&1
[root@kylin-xu ~]# 
[root@kylin-xu ~]# echhhhhh haha >> a.txt 2>&1
[root@kylin-xu ~]# cat a.txt 
hehe
-bash: echhhhhh:未找到命令

第三种写法 echo hehe &>>a.txt
[root@kylin-xu ~]# echo hhaha &>> a.txt 
[root@kylin-xu ~]# echoaaaaaaaa  hhaha &>> a.txt 
[root@kylin-xu ~]# cat a.txt 
hhaha
-bash: echoaaaaaaaa:未找到命令
`` 执行命令
$() 执行命令 和 `` 含义是一样的

[root@kylin-xu ~]# echo `hostname` > a.txt 
[root@kylin-xu ~]# cat a.txt 
kylin-xu
[root@kylin-xu ~]# echo $(hostname -I) > a.txt 
[root@kylin-xu ~]# cat a.txt 
192.168.121.99
[root@kylin-xu ~]# mkdir `hostname`_`hostname -I`
[root@kylin-xu ~]# ll kylin-xu_192.168.121.99/ -d
drwxr-xr-x 2 root root 6 11月  6 09:44 kylin-xu_192.168.121.99/
""
''  单引号里面是什么就输出什么
[root@kylin-xu ~]# echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin
[root@kylin-xu ~]# echo '$PATH'
$PATH
[root@kylin-xu ~]# cat >> 1.txt << EOF
> aa
> aa
> vv
> cc
> EOF
[root@kylin-xu ~]# cat 1.txt 
aa
aa
vv
cc
~     家目录 
.     当前的目录
..    上一级目录
-     上一次所在的目录  su切换用户 更新环境变量
*     表示所有
{}    生成序列
[]    查找序列
?     表示任意单个字符
[root@kylin-xu ~]# ll ?????
-rw-r--r-- 1 root root 12 11月  6 09:46 1.txt
-rw-r--r-- 1 root root 15 11月  6 09:42 a.txt

正则表达式

1) 正则表达式就是为了处理大量的文字|文本|字符串而定义的一套规则和方法。

2) 通过定义的这些特殊符号的辅助,系统管理员就可以快速过滤,替换或输出需要的字符串

3) Linux正则表达式一般以为单位处理的。

简单说

为处理大量文本|字符串而定义的一套规则和方法。

以行为单位出来,一次处理一行。

正则表达式是一种描述一组字符串的模式,类似数学表达式,通过各种操作符组成更小的表达式。

正则表达式- regular expression (RE)

为什么使用正则表达式

1)linux运维工作 大量过滤****(找东西)日志工作。化繁为简。

2)简单,高效,易用。

3)正则表达式高级工具:三剑客 都支持

正则表达式的分类

POSIX规范将正则表达式的分为了两种

1) 基本正则表达式(BRE,basic regular expression)

2)高级功能:扩展正则表达式(ERE,extended regular expression)

【1】、基础正则

# 准备测试数据
I am lizhenya teacher!
I teach linux.
test

I like badminton ball ,billiard ball and chinese chess!
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
my qq num is 593528156

aaaa,
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$
lizhenyalizhenyalizhenya

1、^

以...开头的行

[root@kylin-xu ~]# grep '^my' test 
my blog is http: blog.51cto.com 
my qq num is 593528156
[root@kylin-xu ~]# grep '^^' test 
^^^^^^^^66$$$$$$$^^^$$

2、$

以...结尾的行

[root@kylin-xu ~]# grep 'm$' test 
[root@kylin-xu ~]# grep 'm $' test 
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 

# 注意事项:
有的时候文件的行尾会有空格,这样我们直接按照我们所看到的行末的内容进行过滤就不能过滤出我们想要的内容。此时我们需要看下这一行的末尾的字符是什么,在Linux中文件内容每一行在结束时会加上 $ 。我们就可以看到,my blog is http: blog.51cto.com $ 并不是以m结尾的
[root@kylin-xu ~]# cat test  -A 
I am lizhenya teacher!$
I teach linux.$
test$
$
I like badminton ball ,billiard ball and chinese chess!$
my blog is http: blog.51cto.com $
our site is http:www.lizhenya.com $
my qq num is 593528156$
$
aaaa,$
not 572891888887.$
^^^^^^^^66$$$$$$$^^^$$$
lizhenyalizhenyalizhenya$

# 匹配以空格结尾的行
[root@kylin-xu ~]# grep ' $' test 
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 

^$ 表示匹配空行 grep -v 表示反向过滤

[root@kylin-xu ~]# grep '^$' /etc/selinux/config  -v
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
#SELINUX=disabled
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     ukmls - Multi Level Security protection.
#     ukmcs -ukmcs variants of the SELinux policy.
#SELINUXTYPE=targeted
SELINUXTYPE=targeted
# SETLOCALDEFS= Check local definition changes
SETLOCALDEFS=0

[root@kylin-xu ~]# grep '^$' /etc/selinux/config  -v | grep '^#'  -v
SELINUX=enforcing
SELINUXTYPE=targeted
SETLOCALDEFS=0

3、.

匹配任意单个字符 grep -o 显示匹配过程

[root@kylin-xu ~]# grep '.' a.txt  -o
a
a
a
b
b
x
c
z
a
s
d
f
s
d
a
f
s
d
f
g

4、*

匹配前一个字符出现0次或0次以上

[root@kylin-xu ~]# grep '8*' test  -o 
8
8
88888

.* 表示匹配所有字符,贪婪匹配

image-20241112164759870

5、[]

表示任意单个字符串 还原带含义的特殊符号 . ^ $ 支持序列

grep '[abc]' file  # 不是过滤abc字符的行 过滤或a 或b 或c的行
grep '[a-z]' file
grep '[0-9]' file
grep '[a-Z0-9.^$-]'

# 还原特殊符号
[root@kylin-xu ~]# grep '[-.^$]' test 
I teach linux.
my blog is http: blog.51cto.com 
our site is http:www.lizhenya.com 
not 572891888887.
^^^^^^^^66$$$$$$$^^^$$

[^a] 表示取反,不过滤出 a

笔试题: 统计每个字母出现的次数

[root@kylin-xu ~]# grep '[a-Z]' test -o  | sort  | uniq -c | sort -rn
     17 a
     15 l
     15 i
     14 e
     12 t
     12 n
     11 h
      8 s
      8 o
      7 y
      7 m
      7 c
      6 b
      5 z
      3 w
      3 u
      3 r
      3 I
      3 d
      2 q
      2 p
      2 g
      1 x
      1 k

【2】、扩展正则

扩展正则需要使用 egrep / grep -E

1、+

匹配前面的字符出现1次及1次以上

[root@kylin-xu ~]# egrep '8+' test 
my qq num is 593528156
not 572891888887.
[root@kylin-xu ~]# egrep '8+' test  -o
8
8
88888

2、{}

出现至少n次最多m次

egrep '[0-9]{18}' file  # 连续的18个数字
egrep '8{2,3}'file  # 至少2次,最多3次 优先匹配后面的数字
[root@kylin-xu ~]# egrep '[0-9]{17}[0-9X]' id 
孔 150000123874591242
夏 222113859123487192
赵 37142518322922103X

3、|

表示或者的意思

[root@kylin-xu ~]# egrep 'linux|like' test 
I teach linux.
I like badminton ball ,billiard ball and chinese chess!

4、()

表示一个整体

[root@kylin-xu ~]# egrep 'linu(x|s)' test 
I teach linux.
linux
linus

5、边界符

\b

\<内容\>

[root@kylin-xu ~]# egrep '[0-9]{17}[0-9X]' id
孔 21sf150000123874591242
夏 222113859123487192
赵 37142518322922103X
[root@kylin-xu ~]# egrep '\<[0-9]{17}[0-9X]\>' id
夏 222113859123487192
赵 37142518322922103X
[root@kylin-xu ~]# egrep '\b[0-9]{17}[0-9X]' id
夏 222113859123487192
赵 37142518322922103X
posted @   Linux小菜鸟  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示