(reproduced) use of grep

Source: Internet
Author: User
Tags egrep

R means to recursively grep all files (including subdirectories) under the directory.

For example, grep-r main src will search all files in the SRC directory for the inclusion of the main string.

Therefore, the-R in the Grep-r "main" *.c is meaningless because the input parameter is *.C instead of the directory name.

Xargs cmd executes one cmd for each entry of the standard input input as a parameter,

So find. -name "*.C" | Xargs grep Main is the name of each file found in find with grep main to execute,

For example, find found A.C b.ct est.c so Xargs will be executed three times grep main, respectively grep main a.cgrep main B.cgrep main test.c

The last question you can, with-R, but note that the simple-R will execute on every file in the directory,

Through find, you can execute only the files that you find. But if your grep version is new enough, it might support--include=xxx input parameters,

You can do this. Grep-r--include=*.c main dir will search for Dir and all. c files from the directory, and none of the other files will be searched.

From: http://zhidao.baidu.com/question/146286902.html&__bd_tkn__= 6ca21120346993344f0aa0578dbe36ae8f01dff58078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157 958fb309561a0f85c0f03de0125270ed036b80b3f769b75795abf8ea238330c735d3a5ccb177e3a41b6afde9f0efcaccbdc8c0acd3c23fe46af

Some additions:

This--include option can be used in this way:
Grep-rn--include= ' *.c '--include= ' *.h ' re.

Can be specified more than once, if this is the case above, in fact, can be used
Grep-rn--include= ' *. [CH] ' re.

However, if the source file contains C + + source code, the above method does not work, because only one character can be placed in [].
Grep-rn--include= ' *. {cpp,h} ' is also not possible.
An unquoted expansion is required (performed by bash before grep is completed and can be observed by set-x)

Grep-rn--include=*. {Cpp,h} re.

The expansion of this form of {A, b} in bash ignores whether the corresponding file exists in the current directory. This way, you can avoid finger exhaustion on the command line.

Highlight Display

grep--color=auto ' pattern ' text '

Echo-e ' \e[34mhaha\e[m ' This will output a colored string-E for special handling \e This \e must follow the [symbol

grep common

grep [-acinv] ' search string ' filename

Parameter description:

-A: Search data in binary files in text file mode

-C: Calculates the number of ' search string ' found

-I: Ignores case differences, so case is considered the same

-N: Output line number by the way

-V: Reverse selection, which shows the line without the ' search string ' content!

1. Search for specific characters

grep ' OO ' pp.txt the following only with PP for pp.txt

View the number of lines that display characters

Grep-n ' OO ' pp

To view non-OO characters

Grep-v ' OO ' pp

View uppercase and lowercase characters ignored

Grep-i ' OO ' p

2. Using [] to process search results

viewing strings for tast and test

Grep-n ' T[ae]st ' pp here [ae] only one character to deal with a or e so you can match the requirements above

If you want to match T (x) St This x is any character, then you can do the following

grep ' t[. St ' pp. symbols represent arbitrary characters

View information that contains OO characters

Grep-n ' OO ' pp

If you want to not have G's character information before Oo

Grep-n ' [^g]oo ' pp here ^ is to take the opposite meaning non-goo character

Take the character that precedes the non-character

Grep-n ' [^a-za-z]oo ' pp

To get the characters of the numbers.

Grep-n ' [0-9] ' PP//actually this equates to grep-n ' [0-9[0-9]* ' PP * represents 0 or more duplicate messages

3. Special handling $^ at the beginning and end of the line

If you want to get the first line is the beginning of the word lines

Grep-n ' ^the ' pp

If you want to get characters that begin with an English character

Grep-n ' ^[a-za-z] ' pp

Get information that is not preceded by an English character

Grep-n ' ^[^a-za-z] ' PP//inside ^ is to take the counter outside ^ is to start with the above

Get the line at the end of the decimal point

Grep-n ' \.$ ' PP//decimal point is a special character that needs to be escaped with \

Note ^m This symbol when you change lines under Windows

The way to get a blank line

Grep-n ' ^$ ' pp Here's the empty line.

If you want to get content that is not commented out in all documents, then the following

Grep-v ' ^$ ' pp|grep-v ^# The first is the data that takes a non-empty row the first pipe is data that starts with a non-#

I like sometimes more to take #开头的行

Grep-n ' ^# ' pp

4. Any character and repeating character

. : Absolute arbitrary character

*: 0 or more of the same characters

To see the two-character data in the middle of a GF

Grep-n ' G.. F ' pp

At least one is a string of O

Grep-n ' oo* ' PP//because * represents 0 or more

Start and end with G, at least one o in the middle

Grep-n ' Goo*g '

Find the middle of GG is any character string

Grep-n ' G.*g ' pp here. Represents any character

5. Qualifier {}

See the existence of two strings of o between G and P

Grep-n ' Go\{2,5\}p '

Find at least two characters to create a

Grep-n ' Go\{2,\}p ' pp

Find only two strings

Grep-n ' Go\{2\}p ' pp

6. Important Special Characters

^word the string to be searched for (word) at the beginning of the line!

Example: Grep-n ' ^# ' pp search for the line beginning with #!

word$ string to search (word) at the end of the line!

Example: Grep-n '!$ ' pp will end in line! Print out the line!

. Represents an arbitrary character

Example Grep-n ' G. ' pp will be two characters from the beginning of G print out

\ Escapes special characters

Example: Grep-n \ ' pp search for the line with single quotation marks

*: matches 0 or more characters

Grep-n ' o* ' pp matches characters with 0 or more O

\{n,m\}: Number of Matches

Grep-n ' o\{2\} ' pp prints out two oo characters

[] matches a single character

1.[list]: [ABL] matches any one of the ABL

2.[^XX]: Reverse the character in which only the character is reversed if you want to have multiple characters reversed, you need to look at it.

3.[CHAR1-CHAR2]: Matches data within a range such as [a-z][a-z][0-9]

7. Expanded grep---> egrep this equates to GREP-E

Grep-v ' ^$ ' pp | Grep-v ' ^# '

By means of egrep.

Egrep-v ' ^$|^ '

8. Find the relationship with "or" content:

#查找数字为23或24的内容, and display the content with the line number

Grep-e ' 23|24 ' *-n

9. Find out how many blank lines are in the Data.txt file:

grep ' ^$ ' data.txt-c

10. Query How many other directories are in the current directory:

Ls-l | grep ' ^d '

11. Find the contents of a Data.txt file string trailing a

grep ' a$ ' Data.txt-i-n

$ grep "Sort It" * (#或在所有文件中查询单词 "sort It")


All of the following examples refer to querying in a single file
Row matching
$ grep-c "DATA.F"
$4 (#g r e P Returns the number 4, meaning that there are 4 rows that contain the string "4 8". )
$ grep "DATA.F" (#显示包含 "4 8" string of 4 lines of text)

Displays the number of rows that satisfy the matching pattern:
[Email protected] oid2000]# grep-n 1234 111.txt
1:1234
3:1234ab

6. Exact match
[[email protected] oid2000]# grep "1234\>" 111.txt
1234

7. Query for empty rows, and query for rows that begin or end with a condition.
Use a combination of ^ and $ to query for blank lines. Use the-n parameter to display the actual number of rows
[[email protected] oid2000]# grep-n "^$" 111.txt (return result 2: #说明第二行是空行)
[Email protected] oid2000]# grep-n "^ABC" 111.txt (#查询以abc开头的行)
[Email protected] oid2000]# grep-n "abc$" 111.txt (#查询以abc结尾的行)

8. Match Special characters and query for characters that have special meanings, such as $. ' "* [] ^ | \ + ? , you must add \ before a specific character.
[[email protected] oid2000]# grep "\." 111.txt (#在111. txt in the query contains "." of all rows)
[[email protected] oid2000]# grep "my\.conf" 111.txt (#查询有文件名my. c o n F lines)

9. Querying the Directory
[Email protected] oid2000]# ls–l |grep "^d" (#如果要查询目录列表中的目录)
[Email protected] oid2000]# ls–l |grep "^d[d]" (#在一个目录中查询不包含目录的所有文件)
[Email protected]]# ls–l |grep "^d.....x. X "(#查询其他用户和用户组成员有可执行权限的目录集合)

10. Exclude Yourself

Ps-ef|grep Telnet | Grep-v grep (Extracts the "telnet" process in the displayed process; and discards the grep process in PS)

Several special characters of Egrep

+: At least one or more egrep-n ' Go+d ' pp

? : 0 or one egrep-n ' Go?d ' pp

| : Use or to find Egrep-n ' Go|good ' pp find go or good

(): Find out the group's data egrep-n ' G (o|pp) d ' pp view God or gppd this is similar to [] but stronger than [] is can be multiple characters to change

Egrep-n ' d (r|o) e ' pp ===== grep-n ' d[ro]e ' pp

From: http://www.cnblogs.com/wangkangluo1/archive/2012/04/18/2454959.html

(reproduced) use of grep

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.