table of Contents
1.6 linux basis (f) grep Three Musketeers
grep: Global search REgular expression and Print out the line
Line to match the print; text search tool, based on user-specified "model" for the target text line by line matching check: the role of
Mode: filtering criteria by the regular expression characters and text characters written
grep [OPTIONS] PATTERN [FILE...]
grep root /etc/passwd
grep "$USER" /etc/passwd
grep '$USER' /etc/passwd
grep `whoami` /etc/passwd
grep command options:
--color = auto: matching the color display of the text
-v: show no pattern is matched to the line
-i: Ignore character case
-n: display the line numbers match
The number of rows that match Statistics: -c
-o: Display only the matched character string
-q: silent mode does not output any information
-A #: after, after # line (look for signs)
-B #: before, before the line #
~]# nmap -v -sP 192.168.32.0/24 |grep -B1 up|grep "Nmap scan" |cut -d" " -f5
-C #: context, each longitudinal row #
-e: to achieve a logical relationship between the number of options or
grep –e 'cat ' -e 'dog' file
-w: match whole words
-E: Use ERE, extended regular expressions
-F: the equivalent of fgrep, does not support regular expressions (look fast)
1.6.1 Regular Expressions
REGEXP: a special kind of mode character and text written characters, some characters (metacharacters)
Does not represent a character literal meaning, while a control function or wildcard
Program support: grep, sed, awk, vim, less, nginx, varnish, etc.
Divided into two categories:
The basic regular expressions: BRE
Extended regular expressions: ERE grep -E, egrep
The regular expression engine:
Using different algorithms, check processing software modules regular expression
PCRE(Perl Compatible Regular Expressions)
Metacharacters Category: character, the matching frequency and location of anchors, group
man 7 regex
== Basic == regular expression metacharacters
1.6.1.1 character matches:
. Matches any single character
[] Matches any single character within a specified range
[^] Any single character match outside the specified range
[: alnum:] letters and numbers
[: alpha:] represents any case English characters, i.e. AZ, AZ
[: Lower:] lowercase
[: Upper:] uppercase letters
[: digit:] decimal number
[: punct:] Punctuation
[: space:] the horizontal and vertical whitespace
[: Blank:] white space (spaces and tabs)
[: Space:] vertical and horizontal whitespace (ratio [: blank:] contains a wide range)
[: Cntrl:] unprintable control character (backspace, delete, bell ...)
[: Xdigit:] hexadecimal digits
[: Graph:] non-blank printable characters
[: Print:] printable characters
1.6.1.2 the number of matches
After the character to be used in a specified number of times specified for the preceding character to appear
*: Any number of times in front of the match character, including zero
Greed mode: as long as possible match
* Any character of any length
? Matches its preceding character 0 or 1 times
Match front + character at least once
{N} matches the preceding character n times
{M, n} m character match at least the front and at most n times
{, N} foregoing character match up to n times
{N,} matches at least n times the previous character
1.6.1.2 anchoring position: positioning to appear
- ^ Beginning of a line anchored mode for most of the left
- $ Anchored end of the line, for the rightmost pattern
- ^ PATTERN $ pattern matching for the whole line
- ^ $ Empty line
- ^ [[: Space:]] * $ Blank lines
- <Or \ b word-initial anchor for word patterns of the left
- > Or \ b suffix anchor; right mode for the word
- <PATTERN> matches the entire word
1.6.1.3 Grouping:
() One or more characters tied together, as a whole for processing
Such as: (root) + parentheses packet pattern matching content to be recorded in the regular expression engine of internal variables, these variables are named: \ 1, \ 2 \ 3, ...
\ 1 showing from the left a first mode between the left bracket and a right bracket matching the matched character
Example: (string1 + (string2) *)
\1 :string1+(string2)*
\2 :string2
After the reference to 1.6.1.4
- Reference packet mode in parentheses preceding the matching characters, rather than the pattern itself
[root@centos7 ~]# cat 1.txt
He love his lover.
She like her liker.
He like his lover.
[root@centos7 ~]# cat 1.txt |egrep "(love)+.*\1"
He love his lover.
1.6.1.5 Or: |
- Example: a | b: a or B
C | cat: C or cat
(C | C) AT: Cat or cat
Exercise:
1, the display / proc / meminfo file to the beginning of the line size s (requirement: two methods)
[root@CentOS7 scripts]# cat /proc/meminfo |grep -E "^(s|S)"
SwapCached: 0 kB
SwapTotal: 3145724 kB
SwapFree: 3145724 kB
Shmem: 10640 kB
Slab: 119216 kB
SReclaimable: 59848 kB
SUnreclaim: 59368 kB
[root@CentOS7 scripts]# cat /proc/meminfo |grep "^[sS]"
SwapCached: 0 kB
SwapTotal: 3145724 kB
SwapFree: 3145724 kB
Shmem: 10640 kB
Slab: 119216 kB
SReclaimable: 59848 kB
SUnreclaim: 59368 kB
[root@CentOS7 scripts]# cat /proc/meminfo |grep -i "^s"
SwapCached: 0 kB
SwapTotal: 3145724 kB
SwapFree: 3145724 kB
Shmem: 10640 kB
Slab: 119232 kB
SReclaimable: 59848 kB
SUnreclaim: 59384 kB
2, the display / etc / passwd file not to / bin / bash line end
[root@CentOS7 scripts]# cat /etc/passwd |grep -v "/bin/bash$"
3, displays the user's default shell program rpc
[root@CentOS7 scripts]# cat /etc/passwd |grep "rpc\>"|cut -d : -f7
/sbin/nologin
4, to find the / etc / passwd in two or three digits
[root@CentOS7 scripts]# cat /etc/passwd|grep "\<[1-9][0-9]\{1,2\}\>"
5, display /etc/grub2.cfg file CentOS7, at least to begin with a blank line of characters and there is a non-blank character behind
[root@CentOS7 scripts]# cat /etc/grub2.cfg |grep "^[[:space:]]\+[^[:space:]]\+"
[root@CentOS7 scripts]#cat /etc/grub2.cfg |grep "^[[:space:]]\{1,\}[^[:space:]]\{1,\}"
6, to find the "netstat -tan" command followed by the results to the end of any number of white space characters LISTEN line
[root@CentOS7 scripts]# netstat -tan|grep "LISTEN[[:space:]]*$"
7, displays the user names of all system users on CentOS7 and UID
[root@CentOS7 scripts]# cat /etc/passwd |grep "/sbin/nologin"|cut -d":" -f1,3
8, adding users bash, testbash, line basher, sh, nologin (its shell to / sbin / nologin), find / etc / passwd username and shell of the same name
[root@CentOS7 scripts]# cat /etc/passwd |grep "^\([[:alnum:]]\+\>\).*\1$"
9, and the use of df grep, remove the disk usage of each partition, and descending order
[root@CentOS7 scripts]# df|grep "/dev/sd.*"|tr -s " " |cut -d" " -f1,5 |sort -n
/dev/sda1 16%
/dev/sda2 9%
/dev/sda3 1%
[root@CentOS7 scripts]# df|grep "/dev/sd.*"|tr -s " " |cut -d" " -f1,5|sort -t" " -k2 -nr
/dev/sda1 16%
/dev/sda2 9%
/dev/sda3 1%
[root@CentOS7 ~]# df |grep "/dev/sd"|egrep -o "[0-9]{1,3}%"
1.6.1.6 egrep extended regular expressions
egrep = grep -E
egrep [OPTIONS] PATTERN [FILE...]
Extended regular expression metacharacters:
Character matches:
. Any single character
[] Specified range of characters
[^] Not in the specified range of characters
The number of matches :
* : Matches the previous character any number of times
?: 0 or 1
+: 1 or more times
{M}: m times Match
{M, n}: at least m, up to n times
Anchor position:
^: Beginning of the line
$: End of line
<, \ B: First word
>, \ B: suffix
Packet: ()
After the quote: \ 1, \ 2, ...
Or: a | b: a or b
C | cat: C or cat
(C|c)at:Cat或cat
1.6.2 Exercise
1, shows three users root, mage, wang UID and default shell
[root@CentOS7 scripts]# cat /etc/passwd |egrep "^(root|mage|wang)\>"|cut -d: -f1,3,7
root:0:/bin/bash
mage:4035:/bin/bash
wang:4049:/bin/bash
2, first find out /etc/rc.d/init.d/functions lines in the file for a word (underscore) followed by a row of small parenthesis
[root@CentOS7 scripts]# cat /etc/rc.d/init.d/functions |egrep "^[[:alnum:]_]+\>\("
3, taken egrep using its group name /etc/rc.d/init.d/functions
[root@CentOS7 scripts]# echo /etc/init.d/functions/ |egrep -o "[^/][[:alnum:]_]+/?$"
[root@CentOS7 scripts]# echo /etc/init.d/functdfioj_123/ |egrep -o "[^/]+/?$"
functdfioj_123/
4, the top directory name removed using egrep path
[root@CentOS7 scripts]# echo /etc/init.d/functdfioj_123/ |egrep -o "/.*/?$"
5, each host stats last command logged in as root IP address of logins
[root@CentOS7 scripts]# last |egrep -o "\<([1-9]|[1-9][0-9]|1[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.([1-9]|[1-9][0-9]|1[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.([1-9]|[1-9][0-9]|1[1-9][0-9]|2[0-4][0-9]|25[0-5])\>\.([1-9]|[1-9][0-9]|1[1-9][0-9]|2[0-4][0-9]|25[0-5])\>" |sort|uniq -c
1 172.16.253.196
103 192.168.110.1
1 192.168.110.128
6, using the extended regular expressions represent 0-9,10-99,100-199,200-249,250-255
0-9:[0-9]
10-99:[1-9][0-9]
100-199:1[0-9][0-9]
200-249:2[0-4][0-9]
250-255:25[0-5]
7, shows all IPv4 addresses ifconfig command results
ifconfig |egrep -o "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-5]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-5]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-5]|25[0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-5]|25[0-5])"
[root@centos6 ~]# hostname -I
192.168.24.6
8, this string: welcome to centos linux
Each character and to re-sort, many repetitions discharged to the front
[root@CentOS7 scripts]# echo "welcome to magedu linux "|egrep -o "[[:alpha:]]"|sort |uniq -c|sort -nr
3 e
2 u
2 o
2 m
2 l
1 x
1 w
1 t
1 n
1 i
1 g
1 d
1 c
1 a
echo "welcome to magedu linux"|sed 's/[[:space:]]/\n/g'|sed -r 's/([[:alpha:]])/\1\n/g'|sed '/^$/d'|sort |uniq -c |sort -nr