The grep command of the Shell scripting tool

Source: Internet
Author: User
Tags gopher egrep

Tag: The grep command of the Shell scripting tool

grep (abbreviated from globally search a Regular expression and Print) is a powerful text-search tool for Linux systems that can search for text using regular expressions , and print out the matching lines. Egrep and Fgrep are grep extensions that support more re- metacharacters ,fgrep is fixed grep or fast Grep.linux uses the GNU version of Grep, which is more powerful and can be used with the-G,-e,-f command-line options to use the egrep and fgrep features. grep can be used for shell script , because grep describes the status of the search by returning a status value, or 0 if the template search succeeds, or 1 if the search is unsuccessful, or 2 if the searched file does not exist. We can do some automated text processing with these return values.


Normal expression meta-characters of grep

^ Beginning Locator

$ line Footer Locator

. Match any one character

* Match 0 or more leading characters

[] matches one of the characters in the specified range

[^] matches characters not in range

\< Word Head Locator

/〉 Ending Locator

X\{m\} repeats x characters m times

X\{m,\} repeats x characters at least m times

X\{m,n\} repeats x characters m to n times


File contents:

[email protected] opt]# ll passwd
-rw-r--r--. 1 root root 1087 Mar 17:39 passwd
[email protected] opt]# cat passwd
Root:x:0:0:root:/root:/bin/bash
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
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
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
[Email protected] opt]#


1.GREP command format

grep [option] character mode [filename 1, filename 2 ...]


2. Find rows starting with R

[[email protected] opt]# grep ' ^r ' passwd
Root:x:0:0:root:/root:/bin/bash
[Email protected] opt]#


3. Find the line ending in C

[[email protected] opt]# grep ' C $ ' passwd
Sync:x:5:0:sync:/sbin:/bin/sync
[Email protected] opt]#


4. Look for lines that begin with H, end with a T, and have only two characters in the middle

[[email protected] opt]# grep ' \Halt:x:7:0:halt:/sbin:/sbin/halt
[Email protected] opt]#


5. Find the contents of the file as long as there are h or Q characters

[[email protected] opt]# grep ' [HQ] ' passwd
Root:x:0:0:root:/root:/bin/bash
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
Gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
[Email protected] opt]#


6. Find each line with a to O character appearing 7 times

[[email protected] opt]# grep ' [a-o]\{7\} ' 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
[Email protected] opt]#


7. Find the same content in some files

[[email protected] opt]# grep root passwd*
Passwd:root:x:0:0:root:/root:/bin/bash
Passwd:operator:x:11:0:operator:/root:/sbin/nologin
Passwd1:root:x:0:0:root:/root:/bin/bash
Passwd1:operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] opt]#


8. Display the line number of the grep result

[Email protected] opt]# grep root-n passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
[Email protected] opt]#


9. Display file names that contain characters

[Email protected] opt]# grep root-l passwd
passwd
[Email protected] opt]#


10. Displaying characters in a file

[Email protected] opt]# grep root-c passwd
2
[Email protected] opt]#


11. Find content is a word

[Email protected] opt]# Grep-w halt passwd
Halt:x:7:0:halt:/sbin:/sbin/halt
[Email protected] opt]#


12. Reverse Filtering

[Email protected] opt]# grep-v Bash passwd | Grep-v Nologin passwd
Root:x:0:0:root:/root:/bin/bash
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
[Email protected] opt]#

13. Use-E to interpret wildcard characters

[Email protected] opt]# grep-v-E ' bash|nologin ' passwd
Sync:x:5:0:sync:/sbin:/bin/sync
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Halt:x:7:0:halt:/sbin:/sbin/halt
[Email protected] opt]#

This article is from the "Days Together" blog, please be sure to keep this source http://tongcheng.blog.51cto.com/6214144/1622319

The grep command of the Shell scripting tool

Related Article

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.