1. grep Introduction
Grep (Global Search regular expression_r (re) and print out the line, fully search for regular expressions and print rows) is a powerful text search tool, it can use regular expressions to search for text and print matching rows. UNIX grep families include grep, egrep, and fgrep. The commands of egrep and fgrep are only slightly different from those of grep. Egrep is an extension of grep and supports more re metacharacters. fgrep is fixed grep or fast grep. They regard all the letters as words, that is, the metacharacters in a regular expression represent the literal meaning of the regular expression. They are no longer special. Linux uses GNU grep. It is more powerful and can use egrep and fgrep functions through the-G,-E,-F command line options.
Grep works like this. it searches for string templates in one or more files. If the template contains spaces, it must be referenced. All strings after the template are treated as file names. The search result is sent to the screen without affecting the content of the original file.
Grep can be used in shell scripts because grep returns a status value to indicate the search status. If the template search is successful, 0 is returned. If the search is unsuccessful, 1 is returned, if the searched file does not exist, 2 is returned. We can use these return values to automate text processing.
2. grep usage and common options
Format:
Grep [Options] 'pattern' file ,...
Eg: Find the line about root under/etc/passwd
1 [[email protected] ~]# grep ‘root‘ /etc/passwd2 root:x:0:0:root:/root:/bin/bash3 operator:x:11:0:operator:/root:/sbin/nologin
Common options:
-V reverse: displays the rows that cannot be matched by the mode.
-O only displays the strings matched by the pattern, rather than the entire line.
-I case insensitive (ignore-case)
-E supports extended regular expressions with the same significance as egrep
-A # display the row matched by the pattern and the # Row (after) below it)
-B # display the rows matched by the pattern and the top # Rows (Before)
-C # display the rows matched by the pattern and Upper/lower # Rows
-- Color = auto matches the string to the red
3. metacharacters of the basic Regular Expression
Regular Expression: A pattern written by a type of character. It is used to process the character string in the unit of action. The regular expression is assisted by some special titles, allows you to easily find, delete, and replace a specific string.
Metacharacters: do not represent the meaning of the character itself, used for additional functional descriptions.
(1) character matching
. Any single character
[] A single character in the specified range
[0-9] or [[: digit:] match any single number in the range of 0-9
[A-Z] or [[: lower:] match any single letter in the lowercase letter A-z
[[: Alpha:] match any single letter in the range of A-Z and A-Z
[[: Alnum:] match any single character in the range of uppercase and lowercase letters A-Z, a-Z, and 0-9
[[: Space:] matching Spaces
[[: Punct:] matching punctuation marks
[^] Any single character out of the specified range
(2) times matching is used to specify the number of times to match the character before it
* Any number of times
Eg: x * y ------ XXY, XY, and Y are matched.
. * Match any character of any length
\? 0 or 1 time
Eg: X \? Y ------ XY, Y, XXY
1 [[email protected] ~]# grep --colour=auto "x\?y" xy2 xxy3 xy4 y
The above code matches XXY because the greedy pattern in the matching principle is as long as possible to match characters.
\ {M \} matches m times
\ {M, N \} matches m to n times
\ {M, \} matches at least m times
\ {0, n \} matches up to n times
(3) Positioning
^ Pin the first row ^ char
$ Anchor row tail char $
^ $ Blank line
\ <Char anchor beginning or use \ bchar
Char \> pin the end of the word or use char \ B
Eg: in/etc/passwd, pin the word starting with R
1 [[email protected] ~]# grep ‘\<[rR]‘ /etc/passwd2 root:x:0:0:root:/root:/bin/bash3 operator:x:11:0:operator:/root:/sbin/nologin4 rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin5 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin6 ricci:x:140:140:ricci daemon user:/var/lib/ricci:/sbin/nologin
(4) Group
\(\)
Eg: \ (AB \) * XY indicates that AB appears repeatedly multiple times
(5) Reference
\ 1 back reference, referencing the first 1st left parentheses and the Pattern Matching content in the corresponding right brackets
\ 2 back reference, referencing the first 2nd left brackets and the content matched by the pattern in the corresponding right brackets
...
Eg: \ (A. B \) xy \ 1
File Content: abxy, abbxy, a6bxya6b
Indicates that it can match a6bxya6b.
4. Usage of egrep and metacharacters of extended Regular Expressions
Egrep is an extension of grep. Unlike grep, egrep supports more extended metacharacters.
(1) character matching
. Any single character
[] A single character in the specified range
[^] Any single character out of the specified range
(2) times matching is used to specify the number of times to match the character before it
* Any number of times
? 0 or 1 time
+ Match at least once
{M} matches m times
{M, n} matches m to n times
{M,} matches at least m times
{0, n} matches up to n times
(3) Positioning
^ Pin the first row ^ char
$ Anchor row tail char $
^ $ Blank line
\ <Char anchor beginning or use \ bchar
Char \> pin the end of the word or use char \ B
(4) Group
()
| Or eg: A | B indicates A or B
5. Instance
1. display the rows starting with case s in the/proc/meminfo file;
# grep ^[sS] /proc/meminfo
2. Retrieve users whose default shell is not bash;
# grep -v ‘bash$‘ /etc/passwd |cut -d: -f1
3. Obtain the user with the largest ID number and Bash as the default shell;
# grep ‘bash$‘ /etc/passwd|sort -t: -k3|tail -1|cut -d: -f1
4. the/etc/rc. d/rc. sysinit file starts with #, followed by at least one blank character, and then contains at least one non-blank line;
# grep ‘^#[[:space:]]\{1,\}[^[:space:]]\{1,\}‘ /etc/rc.d/rc.sysinit
5. display the lines starting with at least one blank character in/boot/GRUB/grub. conf;
# grep "^[[:space:]]\{1,\}" /boot/grub/grub.conf
6. Find one or two digits in the/etc/passwd file;
# grep "\<[0-9]\{1,2\}\>" /etc/passwd
7. Locate the integer between 1 and 255 in the ifconfig command result;
# ifconfig | egrep "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"
8. view all the information of the root user on the current system;
# grep "^root\>" /etc/passwd
9. Add bash, testbash, and basher users, and find the users with the same username as the default shell on the current system;
# grep "^\<\([[:alnum:]]\{1,\}\)\>.*\1$" /etc/passwd
10. Find the line ending with "listen" or "established" in the result of the netstat-tan command execution;
# netstat -tan |egrep "\<LISTEN\>[[:space:]]*$|\<ESTABLISHED\>[[:space:]]*$"
11. Retrieve the shells of all users on the current system. The requirement is that each shell is displayed only once and in ascending order;
# cut -d: -f7 /etc/passwd| sort -u
12. Write a mode that can match the IP address
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file
Use of grep and egrep