A Linux command 8 grep advanced article per day

Source: Internet
Author: User
Tags egrep

1 Syntax      grep-[ACINV] 'Search Content string ' filename '
-A Search in text file mode
- C count the number of rows found to match
- I. Ignore Case
- N By the way, output line number
- v reverse selection, which is to find rows without a search string
-e Uppercase, search content string support regular
2 Example
Search has theline, and lose the travel number
$grep-n ' the ' Regular_express.txt

search without the line, and lose the travel number
$grep-nv ' the ' Regular_express.txt

use [] to search for collection characters
[] denotes one of these characters, for example [Ade] denotes a or D or e
$ grep-n ' t[ae]st ' regular_express.txt
8:i can ' t finish the test.
9:oh! The soup taste good!

You can use the ^ symbol To do a prefix within [], representing characters other than the characters in the [].
For example, search for the line where the string without G before OO. Use ' [^g]oo ' as the search string
$ Grep-n ' [^g]oo ' Regular_express.txt
2:apple is my favorite food.
3:Football game isn't use feet only.
:Google is the best tools for search keyword.
goooooogle yes!

[] can be expressed in the range, such as [A-z] for lowercase letters, [0-9] for 0~9, [A-z] is capital letters. [a-za-z0-9] denotes all numbers and English characters. Of course, you can also match ^ to exclude characters.
search for rows that contain numbers
$ Grep-n ' [0-9] ' Regular_express.txt
5:however, this dress was about $ 3183 dollars.
15:you is the "is" menu you are the No.1.

the beginning of the line with the trailing character ^ $. ^ represents the beginning of the line, $ means the end of the line (not the character, is the position) then ' ^$ ' represents a blank line, because only the beginning and end of the line. here ^different from the ^ meaning used inside []. It indicates that the following string is the beginning of the row.
For example, search for the line at the beginning
$ Grep-n ' ^the ' regular_express.txt
12:the symbol ' * ' is represented as star.
Search for lines that start with lowercase letters
$ grep-n ' ^[a-z] ' regular_express.txt
Span>2:apple is my favorite food. 4:this dress doesn ' t fit me. 10:motorcycle is cheap than car. 12:the symbol ' * ' is represented as star. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let ' s go.

Search for lines that begin with a letter that is not English
$ grep-n ' ^[^a-za-z] ' regular_express.txt
1: "Open Source" is a good mechanism to develop programs. : #I am Vbird

$ means that the string in front of it is at the end of the line, such as ' \. ' Said. At the end of a line
the search is at the end. the line
$ grep-n ' \.$ ' regular_express.txt//. is a special symbol for regular expressions, so use \ to escape
Span>1: "Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:football game is isn't use feet only. 4:this dress doesn ' t fit me. 5:however, this dress was about $3183 dollars. 6:gnu is the free air isn't free beer.
.....

Note in Ms the text file generated under the system, and a ^m character is added. So the last character will be the hidden ^m, in the processing Windows
Pay special attention to the following text!
Can be used with Cat Dos_file | Tr-d ' \ r ' > unix_file to delete the ^m symbol. ^m==\r

Then ' ^$ ' means only the empty line at the end of the line!
Search for empty lines
$ grep-n ' ^$ ' regular_express.txt
£ º


Search for non-empty rows
$ grep -vn ' ^$ ' regular_express.txt
Span>1: "Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:football game is isn't use feet only. 4:this dress doesn ' t fit me.
..........

any one character. With repeating characters *

In Bash, * represents a wildcard character, which is used to represent any number of characters, but in a regular expression, he has a different meaning, * indicates that there are 0 or more characters.
such as oo*, indicates that the first o must exist, that the second o can have one or more, or not, so that it represents at least one O.

Point. Represents an arbitrary character that must exist. G?? D can be used with ' G. d ' means. Good, Gxxd, gabd ..... are consistent.

$ Grep-n ' G.. d ' Regular_express.txt
1: "Open Source" is a good mechanism to develop programs. 9:oh! The soup taste good! 16:the World was the same with ' glad '.

search two o above string
$ grep-n  ' ooo* '  regular_express.txt//First two o must exist, the third o may not, there can be more than one.
Span>1: "Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:football game is isn't use feet only. 9:oh! The soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!

Search for G start and end, middle is at least one o string, namely Gog, Goog....gooog ... such as
$ Grep-n ' goo*g ' regular_express.txt
18:google is the best
tools for search keyword. 19:goooooogle yes!

Search G The line at the beginning and end of the string
$ grep-n ' g.*g ' regular_express.txt//. * Denotes 0 or more arbitrary characters
Span>1: "Open Source" is a good mechanism to develop programs. 14:THE GD Software is a library for drafting  Programs. 18:google is the best tools for search keyword. 19:goooooogle yes! 20:go! go! Let ' s go.


limit the range of consecutive repeating characters {} 
. *  can limit only 0 or more, or {range} if you want to limit the exact number of characters to repeat. The range is numeric, separated by 2,5 for 2~5 ,
2 for 2, 2 for 2 to more
Note that because {} has a special meaning in the shell, So use it as a regular expression to escape it.

searches for a line that contains two o strings.
$ grep-n  ' o\{2\} '  regular_express.txt 
Span>1: "Open Source" is a good mechanism to develop programs. 2:apple is my favorite food. 3:football game is isn't use feet only. 9:oh! The soup taste good! 18:google is the best tools for search keyword. 19:goooooogle yes!

Search G Followed by 2~5 an O, followed by a string of G's line.
$ Grep-n ' go\{2,5\}g ' regular_express.txt
18:google is the best
tools for search keyword.


Search contains G Followed by more than 2 O, followed by a G line.
$ grep-n ' go\{2,\}g ' regular_express.txt
18:google is the best
tools for search keyword. 19:goooooogle yes!


pay attention to []^-does not show special meaning, it can be placed in [] behind the content.
' [^a-z\.! ^-] 'indicates no lowercase letters, no. No, no spaces, no--the string, note [] There is a small space inside.

In addition Shellthe reverse selection inside is [!range], and the regular inside is [^range]


2Extending Regular Expressions

an extended regular expression adds several special compositions to the underlying regular expression.
It makes certain operations more convenient.
For example, we want to remove the blank line and the beginning of the #的行, this will be used:
$ Grep-v ' ^$ ' regular_express.txt | grep-v ' ^# '
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
Football game isn't use feet only.
This dress doesn ' t fit me.
............

However, it is much easier to use egrep and extended special symbols that support extended regular expressions.
Note that grep supports only underlying expressions, while Egrep supports extensions, in fact Egrep is an alias for GREP-E. Therefore, the GREP-E supports extended regular.
so:
$ Egrep-v ' ^$|^# ' regular_express.txt
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
Football game isn't use feet only.
This dress doesn ' t fit me.
....................
Here | Represents or the relationship. A string that satisfies ^$ or ^#.

several extended special symbols are listed here:
+, in. * acts Similarly, representing one or more repeating characters. ? , which is similar in. * , represents 0 or one character. |, an expression or a relationship, such as ' Gd|good|dog ' , that represents a string of Gd,good or dog () that synthesizes part of the content into a group of cells. For example, to search for glad or good can be the advantage of ' G (la|oo) d ' () can be used on the group +? * and so on. For example, to search for a and C at the beginning of the end, there is at least one (XYZ) string, you can: ' A (xyz) +c '

A Linux command 8 grep advanced article every day

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.