シェル正規表現(grep)

正規表現の概要

1つの正規表現の定義

  • 正規表現、別名正規表現、正規表現
    在代码中经常简写为regex、regexp或RE
  • 単一の文字列を使用して、特定の構文または文法規則に準拠する一連の文字列を記述および照合します。
    例:メールサーバーがスパムをフィルタリングする場合、正規表現がよく使用されます。

2つの正規表現構成

(1)普通のキャラクター

  • 大文字と小文字、数字、句読点、その他の記号

(2)メタ文字

  • 正規表現で特別な意味を持つ特殊文字

3つの基本的な正規表現-grep、sed命令支持

(1)基本的な正規表現の例

  • 特定の文字を探す
  1. -n行番号を表示します
  2. -大文字と小文字は区別されません
  3. -v逆ルックアップ

-テストファイルを作成します (直接把test.txt的内容复制就行了)

[root@localhost ~]# cat test.txt 
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

-を見つけて、行番号を表示します (之后的操作都是使用test.txt作为模板,可以更加直观一点)

[root@localhost ~]# grep -n 'the' test.txt 
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to th limit.
`直接'the'筛选the进行过滤`

-検索では大文字と小文字は区別されません (每次做的时候可以对照一下模板test.txt)

[root@localhost ~]# grep -in 'the' test.txt 
3:The home of Football on BBC Sport online.
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword.
6:The year ahead will test our political establishment to th limit.
`加了-i ,所以不区分大小写`

-を含まない行の逆検索

[root@localhost ~]# grep -vn 'the' test.txt 
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
3:The home of Football on BBC Sport online.
7:PI=3.141592653589793238462643383249901429
8:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#woooooood #
13:AxyzxyzxyzxyzC
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single.
16:I shouldn't have lett so tast.
`加了-v ,所以变成了反向查找`

(2)角括弧「[]」を使用して集合文字を検索します

[]-文字数に関係なく、1文字のみを表します。これは「または」の関係に相当します
[^]-括弧内の^は否定を意味します-
シャツまたはショートを含む行を検索します (还是利用刚才的测试文件test.txt进行操作)

[root@localhost ~]# grep -n 'sh[io]rt' test.txt 
1:he was short and fat.
2:He was wearing a blue polo shirt with black pants.
`[io],中括号写的是i和o,所以是i和o都可以,都符合条件`

-単一の文字「oo」を繰り返す行を見つけます

[root@localhost ~]# grep -n 'oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`'oo',这里两个o其实和上面的the是同理的`

-「oo」の前に「w」ではない行を検索します

[root@localhost ~]# grep -n '[^w]oo' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`11,12行‘oo’前面不是‘w’,而是‘o’或多个‘o’,所以会显示出来`

-「oo」の前に小文字ではない行を検索します

[root@localhost ~]# grep -n '[^a-z]oo' test.txt 
3:The home of Football on BBC Sport online.
`[^a-z],以a-z开头的小写字母`

-「oo」の前の大文字ではない行を検索します

[root@localhost ~]# grep -n '[^A-Z]oo' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`[^A-Z],同理以A-Z开头的大写字母`

-番号を含む行を検索します

[root@localhost ~]# grep -n '[0-9]' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
`[0-9],包含数字的行`

**(可以按自己的理解试试其他的筛选条件)**

(3)開始文字と終了文字を見つける$

  • 小数点「。」は正規表現のメタ文字です。通常の文字に変換するには、エスケープ文字「\」を使用する必要があります
    。小数点「。」で終わる行を見つけてください。
[root@localhost ~]# grep -n '\.$' test.txt 
`查找空行`
[root@localhost ~]# grep -n '^$' test.txt 

(4)任意の文字「。」および繰り返される文字「*」を検索します

  • 「w」で始まり、4文字の「d」で終わる行を検索します
[root@localhost ~]# grep -n 'w..d' test.txt 
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
`w..d就是中间的两个'.'表示任意字符`
  • 「*」は、先行する単一文字を0個以上繰り返すことを意味します。

-クエリに少なくとも2つのoの文字列が含まれています

[root@localhost ~]# grep -n 'ooo*' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`‘ooo*’---第1个'o'和第二个'o'必须存在,第三个'o'可以是0个或多个,所以oo,ooo等都符合规则`
`同理oo*的话就是第一个'o'必须存在,第二个'o'可以是0个或多个`

-「w」で始まり、少なくとも1つの「o」を含み、「d」で終わる行を検索します

[root@localhost ~]# grep -n 'woo*d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #
`这个就是利用了oo*,第一个'o'必须有,如果是中间至少包含两个'o'的话就是wooo*d`

-「w」で始まり、オプションの文字で「d」で終わる行を検索します

[root@localhost ~]# grep -n 'w.*d' test.txt 
1:he was short and fat.
5:google is the best tools for search keyword.
8:a wood cross!
9:Actions speak louder than words
11:#woood #
12:#woooooood #
`w.*d 这个'.'后面跟了* ,所以这个'.'可以是0个也可以是多个,说白了就是以'w'开头'd'结尾的不管多少字符都符合`

-任意の数の行を照会します

[root@localhost ~]# grep -n '[0-9][0-9]*' test.txt 
4:the tongue is boneless but it breaks bones.12!
7:PI=3.141592653589793238462643383249901429
`[0-9][0-9]*第一个[0-9]必须有,第二个可以是0个也可以是多个,所以可以看成只要有数字不管多少字符的行都符合`

(5)連続する文字の範囲を見つける{}

**使用 " . "和 " * "可以设置零个或无限多个重复的字符,如果要限制一个范围则使用' { } **
-2つのO文字を表示

[root@localhost ~]# grep -n 'o\{2\}' test.txt 
3:The home of Football on BBC Sport online.
5:google is the best tools for search keyword.
8:a wood cross!
11:#woood #
12:#woooooood #
14:I bet this place is really spooky late at night!
`o\{
     
     2\}即查找o并且查找字符范围是2两个及以上`

-wの始まり、dの終わりを見てください。中央は、2〜5oの文字列です。

[root@localhost ~]# grep -n 'wo\{2,5\}d' test.txt 
8:a wood cross!
11:#woood #
`wo\{
     
     2,5\}d,2,5及2-5个字符,开头是w结尾是d的`

-wの先頭、dの末尾、および中央に2つ以上のoが含まれる文字列を確認します。

[root@localhost ~]# grep -n 'wo\{2,\}d' test.txt 
8:a wood cross!
11:#woood #
12:#woooooood #
`wo\{
     
     2,\}d,这里2后面跟了','号,就是说2个以上,不加就是两个,参考上面的案例`

基本的な正規表現の4つの一般的なメタ文字

\b   单词的开头或结尾,只匹配一个位置,不匹配分隔标点符号和空格    
\d   一个数字,等价于 [0-9]    
*      数量,它前面的内容以连续使用的任意次数以达到整个表达式匹配,可以是0次匹配  
+     * 类似,但至少匹配1, 匹配一个或多个     
    和上面两个类似,重复0次或一次
.      匹配除了换行符以外任意字符  
\s    匹配任意的空白符、制表符、换行符、中文全角空格等
\w    匹配字母、数字、汉字或者下划线
^     用来查找的字符串的开头  
$     用来查找的字符串的结尾
{
    
    n}    前面必须连续重复匹配n次,
{
    
    n,m}   前面必须连续重复匹配n~m次,
{
    
    n,}     前面必须连续重复匹配n~更多次,
\   如果需要查找元字符,需要转义 
[]   里面的字符可以不用转义,用来定义匹配集合  

5、拡張正規表現の一般的なメタ文字-egrep、awk命令支持

+		重复一个或者一个以上的前一个字符
		零个或者一个的前一个字符
|		使用或者(or)的方式找出多个字符
()		查找“组”字符串
()+		辨别多个重复的组

おすすめ

転載: blog.csdn.net/rzy1248873545/article/details/110392349