Linux Common Commands-grep,egrep,regexp

Source: Internet
Author: User
Tags gopher egrep

grep: Searches for text based on patterns and displays lines of text that conform to the pattern.
Pattern: Text word wildcards regular the expression's meta-character combination to match the condition


grep [Options] PATTERN [FILE ...]


-I (ignoring case)

[[email protected] ~]# grep root/etc/passwd
Root:x:0:0:root:/root:/bin/bash
Operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] ~]#


--color (plus color)


-V: Show rows that are not matched by the pattern

[Email protected] ~]# grep-v root/etc/passwd
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin


-O: Displays only strings that are matched by the pattern

[Email protected] ~]# Grep-o root/etc/passwd
Root
Root
Root
Root
[Email protected] ~]#



*: Any character of any length
?: any single character
[]:
[^]:


Regular Expressions: REGular expression, REGEXP
Metacharacters
.: Matches any single character
[]: matches any single character within the specified range
[^]: matches any single character outside the specified range
Character set: [:d igit:], [: Lower:], [: Upper:], [:p UNCT:], [: Space:], [: Alpha:], [: Alnum:]

[[email protected] ~]# grep ' [[:d igit:]]$ '/etc/inittab
# 5-x11
[Email protected] ~]#




Number of matches (greedy mode):
*: Matches any of its preceding characters any time
A, B, AB, AAB, ACB, ADB, AMNB
A*b, A?b
A.*b

[[email protected] ~]# grep ' a*b ' test.txt
B
Ab
AaB
Acb
adb
Acdafb
Asdflkbsdfb
[Email protected] ~]#



. *: Any character of any length

[[email protected] ~]# grep ' a.*b ' test.txt
Ab
AaB
Acb
adb
Acdafb
Asdflkbsdfb
[Email protected] ~]#



\?: Match its preceding character 1 or 0 times

[[email protected] ~]# grep ' a\?b ' test.txt
B
Ab
AaB
Acb
adb
Acdafb
Asdflkbsdfb
[Email protected] ~]#


\{m,n\}: Matches the preceding character at least m times, up to N times
\{1,\}: At least once
\{0,3\}:0-3 Times

[[email protected] ~]# grep ' a\{1,3\}b ' test.txt
Ab
AaB
[Email protected] ~]#

[[email protected] ~]# grep ' a.\{1,3\}b ' test.txt
AaB
Acb
adb
Acdafb
[Email protected] ~]#



Location anchoring:
^: Anchor the beginning of the line, any content after this character must appear at the beginning of the line

[[email protected] ~]# grep ' ^r. T '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
[Root[email protected] ~]#


$: Anchor line end, any content in front of this character must appear at the end of the row

[[email protected] ~]# grep ' login$ '/etc/passwd
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Operator:x:11:0:operator:/root:/sbin/nologin
Games:x:12:100:games:/usr/games:/sbin/nologin
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
Ftp:x:14:50:ftp User:/var/ftp:/sbin/nologin
Nobody:x:99:99:nobody:/:/sbin/nologin



^$: Blank Line

[[email protected] ~]# grep ' ^$ '. BASHRC





[[email protected] ~]# grep ' ^$ '. BASHRC | Wc-l
4
[Email protected] ~]#



\< or \b: Anchor word, any character following it must appear as the first word

[[email protected] ~]# grep ' root\> ' test2.txt
This is root
The user is Mroot
Chroot is a command
Mroot is not a word
[Email protected] ~]#

[[email protected] ~]# grep ' root\b ' test2.txt
This is root
The user is Mroot
Chroot is a command
Mroot is not a word
[Email protected] ~]#



\> or \b: anchors the ending, any character preceding it must appear as the tail of the word

[[email protected] ~]# grep ' \<root ' test2.txt
This is root
Rooter is a dog
[Email protected] ~]#

[[email protected] ~]# grep ' \broot ' test2.txt
This is root
Rooter is a dog
[Email protected] ~]#


\<root>\: Anchoring words

[[email protected] ~]# grep ' \<root\> ' test2.txt
This is root
[Email protected] ~]#



Group:
\(\)
\ (ab\) *
Back to reference
\1: Refers to the first opening parenthesis and all the contents of the corresponding closing parenthesis
[2]
[3]

He love his lover.
She like her liker.
He like his lover.

L.. E


[[email protected] ~]# grep ' \ (L.. e\). *\1 ' Test3.txt
He love his lover.
She like her liker.
[Email protected] ~]#



Practice:
1. Display the lines in the/proc/meminfo file that begin with a non-differentiated s;
Grep-i ' ^s '/proc/meminfo
grep ' ^[ss] '/proc/meminfo
2. Display the line ending with Nologin in/etc/passwd;
grep ' nologin$ '/etc/passwd


Remove the default shell for/sbin/nologin user list
grep "nologin$ '/etc/passwd | Cut-d:-f1


Remove the user name of the user whose default shell is bash with the lowest user ID number
grep ' bash$ '/etc/passwd | Sort-n-T:-k3 | head-1 | Cut-d:-f1


3, display/etc/inittab in the beginning of #, and followed by one or more white space characters, followed by any non-whitespace character line;
grep "^#[[:space:]]\{1,\}[^[:space:]]"/etc/inittab


4, display/etc/inittab contains: A number: (that is, two colons in the middle of a number) line;
grep ': [0-9]: '/etc/inittab


5. Display lines that start with one or more whitespace characters in the/boot/grub/grub.conf file;
grep ' ^[[:space:]]\{1,\} '/boot/grub/grub.conf


6. Display a line in the/etc/inittab file that begins with a number and ends with a number that is the same as the beginning number;
grep ' ^\ ([0-9]\). *\1$ '/etc/inittab


Practice:
1, find out a file, 1 digits, or 2-digit number;
grep ' [0-9]\{1,2\} '/proc/cpuinfo
grep--color ' \<[0-9]\{1,2\}\> '/proc/cpuinfo


2. Find the integer between 1-255 in the result of ifconfig command;



3. Find information about the account of the user whose name is student (must appear at the beginning of the line) on the current system, the file is/etc/passwd
grep ' ^student\> '/etc/passwd | Cut-d:-F3
Id-u Student




Regexp:regular EXPression
Pattern:


Regular Expressions:
Basic REGEXP: Basics
Extended REGEXP: Extended


Basic Regular Expressions:
.:
[]:
[^]:


Number of matches:
*:
\?: 0 or 1 times
\{m,n\}: At least m times, up to n times;


.*:


Anchoring:
^:
$:
\<, \b:
\>, \b:


\(\)
\1, \2, \3, ...


grep: A command that uses a pattern defined by a basic regular expression to filter text;
-I.
-V
-O
--color
-E: Using extended regular expressions
-A #:

[[email protected] ~]# grep-a 2 ' ^core ID '/proc/cpuinfo
Core id:0
CPU Cores:1
apicid:0
[Email protected] ~]#


-B #:

[[email protected] ~]# grep-b 2 ' ^core ID '/proc/cpuinfo
Physical id:0
Siblings:1
Core id:0


-C #:
[[email protected] ~]# grep-c 2 ' ^core ID '/proc/cpuinfo
Physical id:0
Siblings:1
Core id:0
CPU Cores:1
apicid:0
[Email protected] ~]#


To extend the regular expression:


Character Matching:
.
[]
[^]


Number of matches:
*:
?:
+: Match the characters in front of it at least 1 times
{M,n}


Location anchoring:
^
$
\<
\>


Group:
(): Group
\1, \2, \3, ...


Or
|: OR
C|cat:cat or cat, C or cat

[Email protected] ~]# grep-e ' C|cat ' Test6.text
Cat
Cat
C
China
[Email protected] ~]#

[[email protected] ~]# grep-e ' (c|c) at ' Test6.text
Cat
Cat
[Email protected] ~]#


[Email protected] ~]# grep-e ' ^[[:space:]]+ '/boot/grub/grub.conf
Root (hd0,0)
kernel/vmlinuz-2.6.32-504.el6.x86_64 ro root=uuid=57d85756-7680-4c7c-9125-6ad67dae2c45 Rd_NO_LUKS rd_NO_LVM LANG=en _us. UTF-8 rd_no_md sysfont=latarcyrheb-sun16 crashkernel=auto keyboardtype=pc keytable=us Rd_NO_DM quiet
Initrd/initramfs-2.6.32-504.el6.x86_64.img
[Email protected] ~]#

GREP-E = Egrep




4. Display all files that end with a number and do not contain a blank in the file name;
LS *[^[:space:]]*[0-9]?????????



Find the number between 1-255 in the/boot/grub/grub.conf file;
\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>


\.


Ifconfig | Egrep ' \< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> '


Ifconfig | Egrep--color ' (\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \>\.) {3}\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> '


IPV4:
Class 5: A B C D E
a:1-127
b:128-191
c:192-223


\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[01][0-9]|22[0-3]) \> (\.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4] \>) {2}\.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) \>



http://mageedu.blog.51cto.com/



grep, Egrep
Fgrep: Regular expressions are not supported




Linux Common Commands-grep,egrep,regexp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.