Shell Three Musketeers–grep

egrep (grep -E) supports extended metacharacters for regular expressions

1. Use regular rules to determine the need [[ ]]

num1=1
[[ $num1 =~ ^[0-9] + $ ]] & amp; & amp; echo "yes" || echo "no"

num3=1b1
[[ $num3 =~ ^[0-9] + $ ]] & amp; & amp; echo "yes" || echo "no"

2, * 0 or more

useradd abrt
grep 'abc*' /etc/passwd

3. \Word-ending positioning symbol

cat a.txt | egrep '\<[Jj]ack\>'

Extension: ” :% s/\<[Jj]ack\>/123/g “

vim jack.txt

Jack JACK JAck jackly

:% s/\<[Jj]ack\>/123/g

4. What does ^ begin with?

grep '^root' /etc/passwd

5. What does $ end with?

grep 'bash$' /etc/passwd

6. . Matches a single character

grep 'r..t' /etc/passwd

grep 'r.t' /etc/passwd

7, .* any number of characters

grep 'r.*t' /etc/passwd

8. [ ] matches any character in square brackets

grep 'Root' /etc/passwd
grep '[Rr]oot' /etc/passwd

9. [ – ] matches a character within the specified range

grep [a-z]oot /etc/passwd

10. [ ^ ] matches characters that are not in the specified group, reverse the meaning

^ inside [] means negation, ^ outside [] means something to start with.

grep '[^0-9]oot' /etc/passwd

grep '[^0-9A-Z]oot' /etc/passwd

grep '[^0-9A-Za-z]oot' /etc/passwd

grep '[a-z]oot' /etc/passwd

grep '^[rc]oot' /etc/passwd

11. Tags after \( \) matching

vim file1.txt

IPADDR=192.168.1.123
GATEWAY=192.168.1.1
NETMASK=255.255.255.0
DNS=114.114.114.114

:% s#\(192.168.1.\)123#\12#

:% s#\(192.\)\(168.\)\(1.\)2#\1\2\35#

:% s#\(192.\)\(168.\)\(1.\)\(5\)#\1\26.\4# 

12. + matches one or more leading characters

egrep 'ro + t' /etc/passwd

13.? Matches zero or one leading character

egrep 'ro?t' /etc/passwd

14. a|b matches a or b

netstat -anlp|egrep ':80|:22'

cat /etc/passwd | egrep 'root|alice'

15, x{m} character x repeated m times

[root@localhost ~]# cat a.txt
love
love.
loove
looooove

cat a.txt | egrep 'o{2}' a.txt

cat a.txt | egrep 'o{2,}' a.txt

cat a.txt | egrep 'o{6,7}' a.txt

16. Extended regular expression metacharacters

+ matches one or more leading characters
[a-z] + ove
? matches zero or one leading character
lo?ve
a|b matches a or b
love|hate
(..)(..)\1\2 label matching characters
(love)able\1er
x{m} character x repeated m times
o{5
x{m,} character x repeated at least m times
o{5,}
x{m,n} character x is repeated m to n times
o{5,10}

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16743 people are learning the system