shell中级-grep

2022/1/14 7:05:16

本文主要是介绍shell中级-grep,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基础正则:

^ 以什么开头  

$ 以什么结尾

 ^$ 空行    

.  任意一个字符     \.为转义“.”     \n  回车     \t   tab建

*   连续出现  

.*  所有

grep --color "^.*REGISTER" /archive/log/tracesbc/tracesbc_sip/tracesbc_sip_1640047578 -n

56277:Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, PRACK, REFER, REGISTER, SUBSCRIBE, UPDATE, PUBLISH

56305:Allow: ACK, BYE, CANCEL, INFO, INVITE, MESSAGE, NOTIFY, OPTIONS, PRACK, REFER, REGISTER, SUBSCRIBE, UPDATE, PUBLISH

56323:REGISTER sip:219.232.39.226 SIP/2.0

56329:CSeq: 1 REGISTER

56343:CSeq: 1 REGISTER

56354:REGISTER sip:219.232.39.226 SIP/2.0

56357:CSeq: 1 REGISTER

56374:CSeq: 1 REGISTER

56391:CSeq: 1 REGISTER

56405:REGISTER sip:219.232.39.226 SIP/2.0

56411:CSeq: 2 REGISTER

56426:CSeq: 2 REGISTER

56437:REGISTER sip:219.232.39.226 SIP/2.0

56440:CSeq: 2 REGISTER

56457:CSeq: 2 REGISTER

56474:CSeq: 2 REGISTER

56502:REGISTER sip:219.232.39.226 SIP/2.0

56508:CSeq: 1 REGISTER

56522:CSeq: 1 REGISTER

56533:REGISTER sip:219.232.39.226 SIP/2.0

56536:CSeq: 1 REGISTER

56553:CSeq: 1 REGISTER

56570:CSeq: 1 REGISTER

56584:REGISTER sip:219.232.39.226 SIP/2.0

56590:CSeq: 2 REGISTER

56605:CSeq: 2 REGISTER

56616:REGISTER sip:219.232.39.226 SIP/2.0

56619:CSeq: 2 REGISTER

56636:CSeq: 2 REGISTER

56653:CSeq: 2 REGISTER

匹配数字

 [0-9]       

匹配小写[a-z]      

匹配大小写

grep '[a-Z0-9]’ redis.conf  

grep  -i '[a-z0-9]’ redis.conf  

取反,例子:取除a、b、c的内容

[^abc]

扩展正则:+ | () {} ?

每一行都有$结尾

cat -A redis.conf

# Set aof rewrite child process to cpu affinity 8,9,10,11:$

# aof_rewrite_cpulist 8-11$

#$

# Set bgsave child process to cpu affinity 1,10,11$

# bgsave_cpulist 1,10-11$

$

# In some cases redis will emit warnings and even refuse to start if it detects$

# that the system is in bad state, it is possible to suppress these warnings$

# by setting the following config which takes a space delimited list of warnings$

# to suppress$

#$

# ignore-warnings ARM64-COW-BUG$

[root@demo1 conf]# sed  -i 's/6381/6882/g' myredis6382.conf |more

[root@demo1 conf]# cat -A redis.conf |more

# Redis configuration file example.$

#$

# Note that in order to read the configuration file, Redis must be$

# started with the file path as first argument:$

#$

# ./redis-server /path/to/redis.conf$

$

# Note on units: when memory size is needed, it is possible to specify$

# it in the usual form of 1k 5GB 4M and so forth:$

#$

# 1k => 1000 bytes$

# 1kb => 1024 bytes$

# 1m => 1000000 bytes$

# 1mb => 1024*1024 bytes$

# 1g => 1000000000 bytes$

# 1gb => 1024*1024*1024 bytes$

查找root在一个文件中,并标记颜色

[root@demo1 conf]# grep --color "root" /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

标记出第几行

[root@demo1 conf]# grep --color -n "root" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

10:operator:x:11:0:operator:/root:/sbin/nologin

grep正则^    例子:以root开头

[root@demo1 conf]# grep --color -n "^root" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

grep正则$    例子:以bash结尾

[root@demo1 conf]# grep --color -n “bash$" /etc/passwd

1:root:x:0:0:root:/root:/bin/bash

过滤出ip地址的行

[root@demo1 conf]# cat test.txt

192.168.3.3

10.0.0.1

111.111.111.11

11111.111.222.333

111.111.111.11.

[root@demo1 conf]# egrep --color "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" test.txt

192.168.3.3

10.0.0.1

111.111.111.11

简写

[root@demo1 conf]# egrep --color "^[0-9]{1,3}\.([0-9]{1,3}\.){2}[0-9]{1,3}$" test.txt

192.168.3.3

10.0.0.1

111.111.111.11

-c统计数量

[root@SBCE1 ipcs]# grep --color -c "sip" /archive/log/tracesbc/tracesbc_sip/tracesbc_sip_1640015158

4348



这篇关于shell中级-grep的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程