Common parameters and usage of grep command

Source: Internet
Author: User
Tags grep regular expression lowercase egrep
1, grep introduction
The grep command is a powerful text search tool in a Linux system that uses regular expressions to search for text and print matching rows. grep full name Global Regular Expr ession Print, which represents the global regular expression version, with permissions that are available to all users. grep is available for shell scripts because grep indicates the state of the search by returning a status value, returns 0 if the template search succeeds, or 1 if the search is unsuccessful, and returns 2 if the searched file does not exist.
2, grep commonly used parameters
-A: binary files are searched for data in the form of text files
-C: Calculates the number of ' match strings ' found
-I: Ignores case differences, so capitalization is considered the same
-N: By the way output line number
-V: Reverse selection, which shows the line with no ' search string ' content.
For example, the Test.txt file reads as follows:
1000 nan
1001 NV 1002-
Nan
Ignore case to match and display line number
Grep-ni ' nan ' test.txt

1:1000 nan
3:1002

Ignore case for "non" match and display line number
Grep-ni ' nan ' test.txt

2:1001 NV

3, grep regular expression
Re Meaning
^word The string to match (word) is at the beginning of the line.
such as: grep ' ^1000 ' test.txt
return: 1000 Nan
word$ The string to match (word) at the end of the line.
such as: grep ' nv$ ' test.txt
Return: 1001 NV
. Represents any one character, it must be any character.
The searched string can be (Eve) (Eae) (EEE) (e e), but not just (EE). There must be only one character between E and E, and spaces is also a character.
such as: grep ' N.N ' test.txt
return: 1000 Nan
\ Escape character, special character Furu ' {}
such as: grep-n \ ' Test.txt
Match the line that contains single quotes.
* Repeat 0 or more of the previous re characters, note that there must be characters in front of the * number
such as: grep ' n ' test.txt
return: 1000 Nan
1001 NV
1002 Nan
\{n,m\} The last re character of continuous N to M
If the \{n\} is a continuous n of the previous re character,
If \{n,\} is a continuous n more than the previous re characters.
[ ] Character set combination of RE special character symbol
[ABC]: means to match A or B or C
[A-Z]: Represents a match a,b,c,... Z any of these 26 letters
[^a-z]: Indicates that the beginning of a string can match a successful one, as long as it is not the lowercase letter A-Z
[a-z$]: Indicates that the end of a string can match a successful one if it is a lowercase letter A-Z


Extended Regular expression: egrep ' pattern ' file.txt or with Grep-e ' file.txt
Re Meaning
+ Repeat "one or more" of the previous re characters
such as: Egrep-n ' go+d ' regular_express.txt
Then: (God) (good) (Goood) etc will match successfully
? The first re character of "0 or one"
such as: Egrep-n ' go?d ' regular_express.txt
Then: (GD) (God) etc will match successfully
| Find a number of strings in or (or)
such as: Egrep-n ' Gd|good ' regular_express.txt
Then: match the GD or good these two strings
() Find the "group" string
such as: Egrep-n ' G (la|oo) d ' regular_express.txt
Then: Match (Glad) or (good) these two strings


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.