[^]: Any single character outside the specified range
650) this.width=650; "Style=" background-image:none;padding-left:0px;padding-right:0px;border-top-width:0px; border-bottom-width:0px;border-left-width:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986313n9qs.png "border=" 0 "height=" 347 "width=" 788 "/>
2, number of times match: used to specify the number of times to match the preceding characters
*: The preceding character any time
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376ixkq.png "border=" 0 "height=" 203 "/>
NOTE: * Any time before the character
. *: matches any character of any length
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376cblu.png "border=" 0 "height=" 331 "/>
\? : The preceding character 0 or 1 times
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376k6yq.png "border=" 0 "height=" 151 "/>
Note: The grep pattern uses greedy mode, that is, to match characters as long as possible;
\{m\}: Matches M-Times
\{m,n\}: matches at least m times, up to N times
\{m,\}: at least m times;
\{0,n\}: Up to n times;
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376gnwt.png "border=" 0 "height=" 295 "/>
3. Position anchoring: used to specify where characters appear
^: Anchoring the beginning of the line
eg. ^char
$: Anchor Line end
eg. char$
^$: Blank line
\<char: Anchor word head, \bchar
char\>: Anchor ending, char\b
4. Group:
\(\)
eg. \ (ab\) *xy
Reference:
\1: A forward reference that references the first opening parenthesis and the corresponding pattern in the closing parenthesis.
\ 2: a back reference, referring to the previous second opening parenthesis, and to the contents of the pattern in the corresponding closing parenthesis
...
eg. \ (a.b\) xy\1:a6bxya6b
650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376fumi.png "border=" 0 "height=" 148 "/>
Note: match the exact match of the reference, and the string is exactly the same
Lab Environment:
Hardware: Thinkpad T430 i5-3320m 4GB RAM 500GB HDD
Virtual machine: 10.0.2 build-1744117
Operating system: CentOS 6.5
1. Display the lines in the/proc/meminfo file that begin with the case s;
# grep "^[ss]"/proc/meminfo
2, remove the default shell for non-bash users;
# grep-v "bash$"/etc/passwd | Cut-d:-f1
3. Remove the user whose default shell is bash and its ID number is the largest;
# grep "bash$"/etc/passwd | Sort-n-T:-k3 | Tail-1 | Cut-d:-f1
4, display/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;
# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"/etc/rc.d/rc.sysinit
5. Display the line beginning with at least one blank character in/boot/grub/grub.conf;
# grep "^[[:space:]]\{1,\}[^[:space:]]\{1,\}"/boot/grub/grub.conf
6, find out the/etc/passwd file in one or two digits;
# grep--color=auto "\<[0-9]\{1,2\}\>"/etc/passwd
7. Find the integer between 1 and 255 in the result of ifconfig command;
# Ifconfig | Grep-e--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|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 user bash and Testbash, basher, and then find the current system on its user name and the default shell of the same user;
# grep--color=auto "^\ ([[: Alnum:]]\{1,\}\) \>.*\1$"/etc/passwd
10. Find the line ending with "LISTEN" or "established" in the result of netstat-tan command execution;
# Netstat-tan | Grep-e--color=auto "(listen| Established) [[: space:]]*$]
11, remove all the users of the current system shell, requirements: Each shell is displayed only once, and in ascending order;
# cut-d:-f7/etc/passwd | Sort-u
12. Write a script that counts the number of rows in the/etc/rc.d/rc.sysinit,/etc/init.d/functions, and/etc/fstab files that begin with # and the number of rows in a blank row;
#!/bin/bashfor file In/etc/rc.d/rc.sysinit/etc/init.d/functions/etc/fstab;do bashline= ' grep-e--color=auto "^#" $fi Le | Wc-l ' spaceline= ' grep-e "^#$" $file | Wc-l ' echo ' $file has $bashline, $file have $spaceline "done
13, write a script, respectively copy/etc/rc.d/rc.sysinit,/etc/init.d/functions and/etc/fstab files to the/tmp directory, the file name is formerly known as the current date composition, (for example, the first file after the name of the copy/ tmp/rc.sysinit-2-14-02-16;)
#!/bin/bashfor path In/etc/rc.d/rc.sysinit/etc/init.d/functions/etc/fstab;do filename= ' basename $path ' CP $path/tmp/$fileName ' date +-%y-%m-%d ' done
14. Write a script
Displays the user name, UID, and line number of all the default shell-bash users on the current system in the/etc/passwd file;
#!/bin/bashcat-n/etc/passwd | grep--color=auto "bash$" | Cut-d:-f1,3
Challenge: Write a pattern that matches the IP address in the real sense; (1.0.0.1--223.255.255.254)
#grep-E--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-2][0-3]) \> (.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-5] \>) {2}.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-4]) \> "
Extension: Write a script to determine whether the IP address entered by the user is legitimate
#!/bin/bashread-p "Enter a IP Address:" Ipaddressif [[-N ' echo $ipAddress | Grep-e "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-2][0-3]) \> (.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-5] \>) {2}.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-4]) \> "']];thenecho" This is a valid IP address! " Elseecho "This is a invalid IP address!" Fi
This article is from the "Wind Qing" blog, please be sure to keep this source http://fengqingyang.blog.51cto.com/9185783/1549640