zl程序教程

您现在的位置是:首页 >  系统

当前栏目

[Linux] Search the contents of files using grep

Linux The of Using search Files grep contents
2023-09-14 08:59:18 时间

Learn the basic syntax for using grep to search the contents of a single file or files. It's like CMD+F, but better! You'll be able to quickly see all matching results in a file.

grep version package.json // seach for version in package.json file
grep version *.json // search for version in all .json files

 

Sometimes you'll be looking for a string, but won't know which file it's in; or you'll want to find all usages of it within a directory. Learn how to use grep -r to recursively search all the files in a directory.

grep -r version . // seach version in current folder
grep -r version examples/react/js // seach version in examples/react/js folder

 


 

Learn to use git grep to only search through the tracked files in a git repo. This is especially useful when you want to exclude build artifacts or locally installed dependencies, such as webpack bundles or the node_modules directory. You'll also note that git grep is automatically colorized - we'll see how to get the same coloring effect with grep --color.

We can highlight the search term by using:

grep -r --color version . // search for version in current folder and highlight the result

 

Sometime it also returns your search from node_modules,  we can use git grep, it will ignore folders and files from .gitignore:

git grep version

 


 

By default, grep displays the file path and the line where a match is found. Learn to use the flags -A, -B, -C, and -n to get more context, and view line numbers as well as the lines surrounding a pattern match.

Add line number:

grep --color -n "#" README.md

 

Add context:

-A 2 // after context 2 lines
-B 2 // before context 2 lines
-C // before and after context 2 lines
grep -n -C 2 --color "#" README.md


 

By using wildcards and glob patterns with the special characters . and *, you can create regular expressions that describe what kind of text you're looking for, instead of just plaintext strings.

.: means any characters 
*: means any length

Example:

grep --color "(.*)" readme.md


 

 

Learn to use grep's extended regular expressions to describe more complex patterns. The ? and + special characters describe optional patterns. The ? character matches zero or one instance of the preceding term, and the + character matches one or more instances of the preceding term. To use these characters, you'll need to either escape them with backslashes or turn on extended regular expressions with the -E flag.

?: 0 or 1
+: 1 or more

If we use:

grep --color "http." readme.md

it can match 'https' or "http:".

Now if we use:

grep --color "https\?" readme.md

It will only match "https" or "http".

We can only do:

grep --color -E "https?" readme.md

If we add '-E' then we don't need '\'., the same rule applies for '+'.

 


 

Describe optional patterns with grep OR using the vertical bar character |. By using the | special character, you can write either-or style patterns. In this lesson we'll look for matches on "grey" or "gray". Like the + and ? characters, the | character is part of extended regular expressions with grep, so you'll either need to escape it with a backslash, or use the -E flag.

grep -r --color "grey\|gray" /examples // find grey or gray in examples folder

We can also do:

grep --color -rE "grey|gray" /examples

 

 


 

Learn to use the special anchor characters ^ and $ to indicate the beginning and end of lines when writing regular expressions for grep. These line anchors are part of basic regular expressions in grep, so you don't need to escape them.

grep --color "^version" /examples  // search start as "version"
grep --color "version$" /examples // search end as "version"

grep --color -rE "^import .* from" . // seach anything begin with import in current dir

 

You can describe classes of characters using bracket expressions. We'll use expressions such as [a-zA-Z] and [0-9] to search for alphabetic and numeric characters with grep.

find . -name "*js" | grep --color "[sS]pec" // find file names contains "js" then in those files only get spec or Spec named file

You can also use some built-in: [[:alpha:]] to match all the [a-zA-Z].

Or [[:xdigit:]] for all the number.

 


 

By grouping your searches with parentheses, you can create even more complex searches. Grouping lets you separate out different parts of your regular expression, and apply special characters like +, ?, and * to groups of search terms.

grep --color -rE "(grey|gray)(\'|\")#?[[:xdigit:]]+" 
// seach fro grey or gray
// follow by ' or "
// follow by # or not
// then has one or more number

It search for all the color in css.


 

What happens when you want to find things that don't contain a pattern? The -v flag lets you use grep in inverse mode. We'll use grep -v against find's output to exclude files in node_modules.

find examples/angularjs -name "*js" | grep -vE "node_modules|Spec" // find all js files excludes node_module and spec