This is the 15th day of my participation in Gwen Challenge
A, rules,
- Grep is used in Linux to use the re
- Match operator
- \ : escape
- . : Matches any single character
- [123a] : Contains any of the characters
- ^ : the beginning of a line
- $: the end of each line
- \
\>
: include this word
- \
[root@node01 ~]# grep -E "\<ooxx\>" grep.txt ooxx 12121212 1212 ooxx 1212 $ooxx Copy the code
- | : or
- () : Choose
- \n: backreference
- See case 2 below
- Repeat operator
- ? : Matches 0 to 1 times
- * : matches 0 to multiple times
- + : matches 1 to multiple times
- {n} : matches n times
- {n,} : matches n to multiple times
- {n,m} : matches n to m times
- Re differences in Linux
- The difference with extended regular expressions
\? , \+, \{, \|, \(, and \)Copy the code
- Of course, the above can also be left out
// Use the -e argument without \ grep -eCopy the code
Second, the case
- Case a
# 1. The data source
ooxx12121212ooxx
ooxx 12121212
oox 12121212
1212 ooxx 1212
oo3xx
oo4xx
ooWxx
oomxx
$ooxx
oo1234xx
ooxyzxx
#2. Contains ooXX data
[root@node01 ~]# grep "ooxx" grep.txt
oxx12121212ooxx
ooxx 12121212
1212 ooxx 1212
$ooxx
#3. Include numbers
[root@node01 ~]# grep "[0-9]" grep.txt
oxx12121212ooxx
ooxx 12121212
oox 12121212
1212 ooxx 1212
oo3xx
oo4xx
oo1234xx
#4. Contains the number 3 or 4
[root@node01 ~]# grep "[34]" grep.txt
oo3xx
oo4xx
oo1234xx
#5. Contains 4-bit integers
[root@node01 ~]# grep -E "[0-9]{4}" grep.txt
oxx12121212ooxx
ooxx 12121212
oox 12121212
1212 ooxx 1212
oo1234xx
#6. The line containing the word ooxx
[root@node01 ~]# grep "\<ooxx\>" grep.txt
ooxx 12121212
1212 ooxx 1212
$ooxx
#7. A line containing only four digits[root@node01 ~]# grep -E "[^0-9][0-9]{4}[^0-9]" grep.txt Oo1234xx //7.2 Analyzing Data and improving [root@node01 ~]# grep -E "(^[0-9]|[^0-9][0-9])[0-9]{2}([0-9]$|[0-9][^0-9])" grep.txt 1212 OOXX 1212 OO1234xx //7.3 Grep"\ [^ \ | [0-9] [^ 0-9] [0-9] \] \ [0-9] {2 \} \ ([0-9] [^ 0-9] \ | $\ [0-9])" grep.txt
Copy the code
- Case 2
# 1. The data source
aaabbcaaa
aa bbc aaa
bb bbc bbb
asgodssgoodsssagodssgood
asgodssgoodsssagoodssgod
sdlkjflskdjf3slkdjfdksl
slkdjf2lskdjfkldsjl
#2. Words that begin with AAA
[root@node01 ~]# grep "\<aaa" test
aaabbcaaa
aa bbc aaa
# 3. Aaa words
[root@node01 ~]# grep "\
" test
\>
aa bbc aaa
# 4. Find out asgodssgoodsssagodssgood//4.1 initial version [root@node01 ~]# grep .*god.*good.*god.*good.* testasgodssGoodsssagodssGood# grep ".*\(god\).*\(good\).*\1.*\2" test
asgodssgoodsssagodssgood
# 5. Find out asgodssgoodsssagoodssgod<! -- [root@node01 ~] -- [root@node01 ~]# grep ".*\(god\).*\(good\).*\2.*\1" test
asgodssgoodsssagoodssgod
Copy the code