Linux大神养成之正则表达式(grep,sed)

Linux大神养成之正则表达式(grep,sed)

分类:linux

Linux大神养成之正则表达式grep命令

参数:

-A后面加数字,代表after,表示把该行的后续n行也列出来-B后面加数字,代表befer,表示把改行的前面n行也列出来$ dmesg color=auto ‘eth’查找特定字符串$ cat -n regular_express.txt 1 “Open Source” is a good mechanism to develop programs. 2 apple is my favorite food. 3 Football game is not use feet only. 4 this dress doesn’t fit me. 5 However, this dress is about $ 3183 dollars.^M 6 GNU is free air not free beer.^M 7 Her hair is very beauty.^M 8 I can’t finish the test.^M 9 Oh! The soup taste good.^M10 motorcycle is cheap than car.11 This window is clear.12 the symbol ‘*’ is represented as start.13 Oh!My god!14 The gd software is a library for drafting programs.^M15 You are the best is mean you are the no. 1.16 The world <Happy> is the same with “glad”.17 I like dog.18 google is the best tools for search keyword.19 goooooogle yes!20 go! go! Let’s go.21 # I am VBird$ grep -n ‘the’ regular_express.txt

8:I can’t finish the test.^M 12:the symbol ‘*’ is represented as start. 15:You are the best is mean you are the no. 1. 16:The world is the same with “glad”. 18:google is the best tools for search keyword.

2.利用中括号来查找集合字符

$ grep -n ‘t[ae]st’ regular_express.txt

8:I can’t finish the test.^M 9:Oh! The soup taste good.^M 查找包含tast或test的字符的句子

$ grep -n ‘[^g]oo’ regular_express.txt

2:apple is my favorite food. 3:Football game is not use feet only. 18:google is the best tools for search keyword. 19:goooooogle yes! ^反向选择,即非的意思,就是oo的前面的字母不是g的

$ grep ‘[^a-z]oo’ -n regular_express.txt

3:Football game is not use feet only. oo前面是非小写的

3.行首与行尾符^$ $ grep -n ‘^[a-z]’ regular_express.txt

2:apple is my favorite food. 4:this dress doesn’t fit me. 10:motorcycle is cheap than car. 12:the symbol ‘*’ is represented as start. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let’s go. ^放在外面表示行首的意思,表示选出以小写字母开始的行

$ grep -n ‘^[^a-zA-Z]’ regular_express.txt

1:”Open Source” is a good mechanism to develop programs. 21:# I am VBird 不是以字母开头的行

$ grep -n ‘\.$’ regular_express.txt

1:”Open Source” is a good mechanism to develop programs. 2:apple is my favorite food. 3:Football game is not use feet only. 4:this dress doesn’t fit me. 10:motorcycle is cheap than car. 11:This window is clear. 12:the symbol ‘*’ is represented as start. 15:You are the best is mean you are the no. 1. 16:The world is the same with “glad”. 17:I like dog. 18:google is the best tools for search keyword. 20:go! go! Let’s go. $表示行尾,表示以 ‘.’ 结尾的行,其中 ‘\’ 是转义符

$ grep -v ‘^$’ /etc/syslog.conf | grep ‘^#’

# Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.*/dev/console # Log anything (except mail) of level info or higher. # Don’t log private authentication messages! # The authpriv file has restricted access. # Log all the mail messages in one place. # Log cron stuff # Everybody gets emergency messages # Save news errors of level crit and higher in a special file. # Save boot messages also to boot.log 选出非空行的,然后在选出以#开头的文字

4.任意一个.和重复的*

$ grep -n ‘g..d’ regular_express.txt

1:”Open Source” is a good mechanism to develop programs. 9:Oh! The soup taste good.^M 16:The world is the same with “glad”. ‘g..d’表示g和d中间有两个连续任意字符的都可以匹配

$ grep -n ‘goo*g’ regular_express.txt

18:google is the best tools for search keyword. 19:goooooogle yes! ‘goo*g’前一个o必须有,后一个’o *’表示一个或多个,,所以为至少一个o

$ grep -n g.*g regular_express.txt

1:”Open Source” is a good mechanism to develop programs. 14:The gd software is a library for drafting programs.^M 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let’s go. 表示由g开头g结尾的行

$ grep -n ‘[0-9][0-9]*’ regular_express.txt

5:However, this dress is about $ 3183 dollars.^M 15:You are the best is mean you are the no. 1. 表示以数字开头,后面有0个到任意个数字的行

5.限定连续RE字符的范围{}

$ grep -n ‘o\{2\}’ regular_express.txt

一错再错,把握正确的方向,

Linux大神养成之正则表达式(grep,sed)

相关文章:

你感兴趣的文章:

标签云: