Linux finds all files containing a certain string and displays the line number Axing stack

 

If you want to find the "hello, world!" string in the current directory, you can do this:

 

grep -rn "hello,world!" *

 

* : Indicates all files in the current directory, or a file name

-r is recursive search

-n is to display the line number

-R finds all files containing subdirectories

-i ignore case

Here are some interesting command line arguments:

 

grep -i pattern files : Search case-insensitively. By default case sensitive, 

grep -l pattern files : list only matching filenames, 

grep -L pattern files : list unmatched filenames, 

grep -w pattern files : only match whole words, not parts of strings (e.g. match 'magic', not 'magical'), 

grep -C number pattern files : [number] lines are displayed for matched contexts, respectively, 

grep pattern1 | pattern2 files : show lines matching pattern1 or pattern2, 

grep pattern1 files | grep pattern2 : Displays lines that match both pattern1 and pattern2. 

There are also some special symbols for searching:

 

\< and \> mark the beginning and end of a word, respectively.

E.g: 

grep man * will match 'Batman', 'manic', 'man', etc., 

grep '\<man' * matches 'manic' and 'man', but not 'Batman', 

grep '\<man\>' matches only 'man', not other strings like 'Batman' or 'manic'. 

'^': means that the matched string is at the beginning of the line, 

'$': means the matched string is at the end of the line,  

 

2, xargs with grep to find

find -type f -name '*.php'|xargs grep 'GroupRecord'

 

Setting line numbers is simple.

We have to go to command mode and enter set number

:set number

close line number

Very simple, we just enter set nonumber

:set nonumber

 

 

Quote: http://151wqooo.blog.51cto.com/2610898/1162118

If you want to find the "hello, world!" string in the current directory, you can do this:

 

grep -rn "hello,world!" *

 

* : Indicates all files in the current directory, or a file name

-r is recursive search

-n is to display the line number

-R finds all files containing subdirectories

-i ignore case

Here are some interesting command line arguments:

 

grep -i pattern files : Search case-insensitively. By default case sensitive, 

grep -l pattern files : list only matching filenames, 

grep -L pattern files : list unmatched filenames, 

grep -w pattern files : only match whole words, not parts of strings (e.g. match 'magic', not 'magical'), 

grep -C number pattern files : [number] lines are displayed for matched contexts, respectively, 

grep pattern1 | pattern2 files : show lines matching pattern1 or pattern2, 

grep pattern1 files | grep pattern2 : Displays lines that match both pattern1 and pattern2. 

There are also some special symbols for searching:

 

\< and \> mark the beginning and end of a word, respectively.

E.g: 

grep man * will match 'Batman', 'manic', 'man', etc., 

grep '\<man' * matches 'manic' and 'man', but not 'Batman', 

grep '\<man\>' matches only 'man', not other strings like 'Batman' or 'manic'. 

'^': means that the matched string is at the beginning of the line, 

'$': means the matched string is at the end of the line,  

 

2, xargs with grep to find

find -type f -name '*.php'|xargs grep 'GroupRecord'

 

Setting line numbers is simple.

We have to go to command mode and enter set number

:set number

close line number

Very simple, we just enter set nonumber

:set nonumber

 

 

Quote: http://151wqooo.blog.51cto.com/2610898/1162118

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324710904&siteId=291194637