grep and sed support basic regular expressions in common file processing tools on Linux systems.
grep - Regularly expresses commands to filter file contents.
Options:
- -i: case insensitive when searching;
- -v: reverse search, showing all columns that do not meet the search criteria;
- -n: Display the line number of the output;
- -A: You can append a number to the end, meaning after. In addition to listing this line, the following n lines are also listed.
- -B: Contrary to'-A', it is listed in the preceding n rows in addition to that row;
Example (in the output after the command executes, what is red is what is found):
[root@localhost ~]# dmesg | grep 'Ebtables' #dmesg lists the core information and then filters out rows containing the'Ebtables'character [ 18.440389] 'Ebtables' v2.0 registered [root@localhost ~]# dmesg | grep -n -A3 -B2 'Ebtables' #This is the line number that will be filtered and lists the'Ebtables'line along with the next three and the first two lines 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 #Find the "bash" character in the file, and prompt: All users who have the "bash" character can log on to the system. 1:root:x:0:0:root:/root:/bin/bash 43:lisi:x:1000:1000:lisi:/home/lisi:/bin/bash [root@localhost ~]# Grep-in'pr[i o]'/etc/passwd "pr" followed by either I or O lines 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 #Find the line in the file that begins with the and display the line number 4:'the' tongue is boneless bu it breaks bones.12! [root@localhost /]# grep -n '^[a-z]' test.txt #Search for lines in a file that begin with a lowercase letter 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 #Search for lines that do not begin with letters 11:#woood # 12:#woooooood # [root@localhost ~]# grep -n "\.$" test.txt #Search for rows ending in. 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 #Show blank lines 10: 17:
Special symbols in regular expressions and their meaning (most can be expressed in another way, here is just a record for future reference):
Basic Regular Expression - Decimal Points and Asterisks
- (decimal point): stands for an arbitrary character;
- * (asterisk): Repeats the previous character, meaning 0 to infinite (0 means or no previous character).
Examples of use:
[root@localhost ~]# grep -n 'sh..t' test.txt #Query begins with sh and ends with t, with at least two characters in between 1:he was 'short' and fat. 2:He was wearing a blue polo 'shirt' with black pants. [root@localhost ~]# grep -n 'wo*' test.txt #Find rows that start with w and end with 0 or infinite 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 #
Since the above display is not friendly, I'll also flag the filter results in red, so I'll take a screenshot next.
Look at the following results for yourself:
Summary of grep commands - Character Summary of Basic Regular Expressions
egrep command - extended regular expression
Generally speaking, basic regular expressions are sufficient for us to use, but if you want to simplify the entire instruction, you can use extended regular expressions. If you use extended regular expressions, you need egrep or awk commands. Common metacharacters of extended regular expressions include the following:
Extended Regular Expression Character Summary: