Home  >  Article  >  php教程  >  grep common commands

grep common commands

高洛峰
高洛峰Original
2016-12-13 14:34:201134browse

This --include option can be used like this:
grep -rn --include='*.c' --include='*.h' re .

can be specified multiple times. If this is the case above, In fact, you can use
grep -rn --include='*.[ch]' re .

However, if the source file contains C++ source code, the above method will not work, because only one can be placed in [] Characters.
grep -rn --include='*.{cpp,h}' is also not possible.
At this time, expansion without quotation marks is required (this has been completed before grep is executed by bash, and can be observed through set -x)

grep -rn --include=*.{cpp,h} re .

The expansion of {A,B} in bash will ignore whether the corresponding file exists in the current directory. This way of writing can Avoid tired fingers on the command line.

Highlight

grep --color=auto 'pattern' 'text'

echo -e 'e[34mhahae[m' This will output colored output String -e means special processing e. This e must be followed by [symbol

grep commonly used

grep [-acinv] 'Search string' filename

Parameter description:

-a: Binary file as text Search data in the file way

-c: Count the number of times the 'search string' is found

-i: Ignore the difference in case, so the case is considered the same

-n: Output the line number by the way

-v: Reverse selection, that is, display the row without 'search string' content!

1. Search for specific characters

grep 'oo' pp.txt Hereinafter only use pp to represent pp.txt

View the number of lines showing characters

grep -n 'oo' pp

View non-oo characters

grep -v 'oo' pp

View characters that are ignored in upper and lower case

grep -i 'oo' p

2. Use [] to process search results

View the strings of taste and test

grep -n 't[ae]st' pp Here [ae] is only treated as a character a or e, so that it can match the above requirements

If you want to match t( x)st If x is any character, you can do it as follows

grep 't[.]st' pp . The symbol represents any character

View information containing oo characters

grep -n 'oo ' pp

If you want there to be no g character information before oo

grep -n '[^g]oo' pp The ^ here means negating the non-goo character

Take the preceding non-character Characters

grep -n '[^a-zA-Z]oo' pp

If you get the characters of numbers

grep -n '[0-9]' pp // In fact, this is equivalent to grep -n ' [0-9[0-9]*' pp * represents 0 or more repeated information

3. Special processing of the beginning and end of the line $^

If you want to get the first line: The character lines starting with the

grep -n '^the' pp

If you want to get the characters starting with English characters

grep -n '^[a-zA-Z]' pp

The result is not Information starting with English characters

grep -n '^[^a-zA-Z]' pp // The ^ inside is the inversion and the ^ outside is the inversion starting with the above

Get the lines ending with the decimal point

grep -n '.$' pp //The decimal point is a special character that needs to be escaped

Pay attention to the ^M symbol when breaking a line under Windows

How to get a blank line

grep -n '^$ ' pp Here is to take the blank lines

If you want to get the non-commented content in all documents, then you can do the following

grep -v '^$' pp|grep -v ^# The first one is to get the non-blank lines The first pipeline of data is to take the data that does not start with #

I think sometimes it is more likely to take the lines that start with #

grep -n '^#' pp

4. Any characters and repeated characters

. : Absolutely any character

* : 0 or more identical characters

To view data with two characters in the middle of gf

grep -n 'g..f' pp

At least There is a string of o

grep -n 'oo*' pp //Because * represents 0 or more

starts and ends with g, and there is at least one o

grep -n 'goo* g'

Find a string with any character in the middle of gg

grep -n 'g.*g' pp Here. It represents any character

5. Qualifier {}

View g and p There are two strings with consecutive o's between

grep -n 'go{2,5}p'

Find at least two characters

grep -n 'go{2,}p' pp

Find a string with only two characters

grep -n 'go{2}p' pp

6. Important special characters

^word The string to be searched for (word) is in Beginning of the trip!

Example: grep -n '^#' pp searches for the line starting with #!

word$The string to be searched (word) is at the end of the line!

Example: grep -n '!$' pp prints the line ending with !!

. Represents any character

Example; grep -n 'g.' pp will print out the two characters starting with g

Escape special characters

Example: grep -n ' pp Searches for the line with single quotes

*: matches 0 or more characters

grep -n 'o*' pp matches characters with zero or more o

{n,m}: The number of matches

grep -n 'o{2}' pp prints out the characters with two oo characters

[] matches a single character

1.[list] : [abl] Match any one in abl

2.[^xx]: Negate the characters. Only one character can be negated here. If you want to negate multiple characters, you need to look at

3.[char1 -char2]: Match data within a certain range, such as [a-z][A-Z][0-9]

7. Extended grep --- >egrep This is equivalent to grep -E

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

expressed through egrep is

egrep -v '^$|^'

8. Find content with "or" relationship:

#Find the content with the number 23 or 24, and display the content and line number

grep -E '23|24' * -n

9. Find how many blank lines there are in the data.txt file:

grep ' ^$' data.txt -c

10. Query how many directories there are in the current directory:

ls -l | grep '^d'

11. Find the data.txt file string ending with Contents of a

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

$ grep "sort it" * (#or query for the word "sort it" in all files)


All examples that follow It refers to querying in a single file
Line matching
$ grep -c "48" data.f
$ 4                                                                                                                                                                                                                                                            48" data.f (#Display 4 lines of text containing the "4 8" string)

Display the number of lines that meet the matching pattern:

[root@mypc oid2000]# grep -n 1234 111.txt
1: 1234
3:1234ab

6. Exact match

[root@mypc oid2000]# grep "1234>" 111.txt
1234

7. Query for empty lines and lines that start or end with a certain condition.

Use ^ and $ together to query empty lines. Use the -n parameter to display the actual number of lines
[root@mypc oid2000]# grep -n "^$" 111.txt (return result 2: #Indicates that the second line is a blank line)
[root@mypc oid2000]# grep - n "^abc" 111.txt (#Query the lines starting with abc)
[root@mypc oid2000]# grep -n "abc$" 111.txt (#Query the lines ending with abc)

8. Match special Characters, query characters with special meanings, such as $ . ' " * [] ^ | + ? , which must be added before specific characters.

[root@mypc oid2000]# grep "." 111.txt (#在111.txt Query all lines containing ".")
[root@mypc oid2000]#grep "my.conf" 111.txt (#Query the lines with the file name my. c o n f)

9. Directory query

[root@ mypc oid2000]# ls –l |grep “^d” (#If you want to query the directory in the directory list)
[root@mypc oid2000]# ls –l |grep “^d[d]” (#In a directory Query all files that do not contain directories)
[root@mypc]# ls –l |grep “^d…..x..x” (#Query the set of directories that other users and user group members have executable permissions)

10. Exclude itself

ps -ef|grep telnet | grep -v grep (extract the "telnet" process among the displayed processes; and discard the grep process in ps)

egrep's few special characters

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

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

| : Use or to search egrep -n 'go| good' pp Find go or good

(): Find the group data egrep -n 'g(o|pp)d' pp Check god or gppd This is similar to [] but more powerful than [] It is to swap multiple characters

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:grep linux commandNext article:grep linux command