Regular expressions (basic and extended)

Common in the Linux file system processing tools grep and sed to support basic regular expressions.

grep-- regular expression command, used to filter the file contents.

Options:

  • -i: does not distinguish between uppercase and lowercase letters when searching;
  • -v: reverse lookup, will not meet the search criteria are displayed in the column;
  • -n: outputs the result of line numbers;
  • -A: can be followed by a number, after the meaning of, in addition to listing the outer row behind the row n is also listed;
  • -B: the role of "-A" contrary, it is in addition to the line, in front of the n-th row is also listed;

Example (the output following the command execution is found labeled red content):

[root@localhost ~]# dmesg | grep 'Ebtables'    #dmesg是列出核心信息,然后过滤出 包含'Ebtables' 字符的行
[   18.440389] 'Ebtables' v2.0 registered
[root@localhost ~]# dmesg | grep -n -A3 -B2 'Ebtables'  
#这是将过滤的内容显示行号,并且列出 'Ebtables' 该行,以及它的后面三行以及前面两行
1773-[    7.850479] NET: Registered protocol family 40
1774-[   18.203047] ip6_tables: (C) 2000-2006 Netfilter Core Team
1775:[   18.440389] 'Ebtables' v2.0 registered
1776-[   18.510067] nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
1777-[   18.714192] bridge: automatic filtering via arp/ip/ip6tables has been deprecated. Update your scripts to load br_netfilter if you need this.
1778-[   18.783253] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[root@localhost ~]# grep -in 'bash' /etc/passwd  
#查找该文件中的“bash”字符,提示:拥有“bash”字符的都是可以登录到系统的用户。
1:root:x:0:0:root:/root:/bin/bash
43:lisi:x:1000:1000:lisi:/home/lisi:/bin/bash
[root@localhost ~]# grep -in 'pr[io]' /etc/passwd   “pr”后面要么是i要么是o的行
14:systemd-bus-'pro'xy:x:999:997:systemd Bus 'Pro'xy:/:/sbin/nologin
28:rtkit:x:172:172:RealtimeKit:/'pro'c:/sbin/nologin
41:sshd:x:74:74:'Pri'vilege-separated SSH:/var/empty/sshd:/sbin/nologin
[root@localhost /]# grep -n '^the' test.txt    #查找文件中以the开头的行,并显示行号
4:'the'  tongue is boneless bu it breaks  bones.12!
[root@localhost /]# grep -n '^[a-z]' test.txt     #搜索文件中以小写字母开头的行
1:he was short and  fat.
4:the  tongue is boneless bu it breaks  bones.12!
5:google is the best  tools for search keyword.
8:a wood cross!
[root@localhost ~]# grep -n ^[^a-zA-Z] test.txt     #搜索不以字母开头的行
11:#woood #
12:#woooooood #
[root@localhost ~]# grep -n "\.$" 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.
5:google is the best  tools for search keyword.
6:The year  ahead will  test  our political  establishment to the limit.
15:Misfortunes  never come  alone/single.
16:I shouldn't have lett  so tast.
[root@localhost ~]# grep -n "^$" test.txt     #显示空行
10:
17:

Regular special symbols and their significance in the expression (most can be expressed in another way, here just to make a record, for future reference):

Regular expressions (basic and extended)

Basic regular expressions - decimal point and asterisk

  • . (Decimal point): there must be a representative of any of the characters;
  • * (Asterisk): a character can be repeated from zero to infinite meaning (0 means that the character can not front) before representatives repeated.

Example of use:

[root@localhost ~]# grep -n 'sh..t' test.txt    #查询以sh开头,以t结尾,中间最少有两个字符的行
1:he was 'short' and  fat.
2:He was  wearing a blue  polo 'shirt' with  black pants.
[root@localhost ~]# grep -n 'wo*' test.txt    #查找以w开头,后面有0个或无穷多个o的行
1:he 'w'as short and  fat.
2:He 'w'as  'w'earing a blue  polo shirt 'w'ith  black pants.
5:google is the best  tools for search key'wo'rd.
6:The year  ahead 'w'ill  test  our political  establishment to the limit.
8:a 'woo'd cross!
9:Actions speak louder  than  'wo'rds
11:#'wooo'd #
12:#'wooooooo'd #

Due to the above display, not friendly, I would like to filter the results marked red, so then I was on the screenshot.

Regular expressions (basic and extended)

Regular expressions (basic and extended)

Look up the results of the following:

Regular expressions (basic and extended)

Regular expressions (basic and extended)
Regular expressions (basic and extended)

Regular expressions (basic and extended)

grep command summary - basic regular expression characters Summary

Regular expressions (basic and extended)

egrep command - extended regular expression

Generally sufficient basis for regular expressions we use, but if you want to simplify the instructions, then you can use extended regular expressions, if you use extended regular expressions, egrep or awk commands need to use common extended regular expressions The main metacharacters include the following:

Extended regular expression characters Summary:
Regular expressions (basic and extended)

Guess you like

Origin blog.51cto.com/14154700/2432199