[Linux] Search the contents of files using grep
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 // after context 2 lines
-B // before context 2 lines
-C // before and after context 2 lines
grep -n -C --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.
?: or
+: 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
[Linux] Search the contents of files using grep的更多相关文章
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- Linux:Day5 shell编程初步、grep
bash的基本特性(3) 1.提供了编程环境 程序编程风格: 过程式:以指令为中心,数据服务于指令: 对象式:以数据为中心,指令服务于数据: shell程序:提供了编程能力,解释执行:过程式.解释执行 ...
- 【Linux】- 对find,xargs,grep和管道的一些理解
问题 相信大家都知道在目录中搜索含有固定字符串文件的命令: find . -name '*.py' |xargs grep test 刚开始的时候,我不熟悉xargs命令,所以直接使用的命令是: fi ...
- Linux中Too many open files 问题分析和解决
今天某个服务的日志中出现了大量的异常: [WARN ] 2018-06-15 16:55:20,831 --New I/O server boss #1 ([id: 0x55007b59, /0.0. ...
- linux文本处理三剑客(一)---grep
在linux种有一句话,叫做一切皆文件.文件有个特点,它是个文本.它可以读.可以写,如果是二进制文件,它还能执行.这样的话,我们在使用linux的时候,多数时间都是要和各式各样文件打交道的.那么,熟悉 ...
- linux基础知识汇总(四)--ps grep命令
转:http://www.cnblogs.com/allen8807/archive/2010/11/10/1873843.html http://www.cnblogs.com/end/archiv ...
- 【Linux】Too many open files
ZA 的BOSS 最近出现Too many open files 异常,这个异常一般是由于打开文件数过多引起, 最常见原因是某些连接一致未关闭 记录一些排查用到的指令 查看每个用户最大允许打开文件数量 ...
- linux命令学习(1):grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- EnCase v7 search hits in compound files?
I used to conduct raw search in EnCase v6, and I'd like to see if EnCase v7 raw search could hit key ...
随机推荐
- PHP类中的__get()和__set函数到底有什么用
PHP类中的__get()和__set函数到底有什么用 一.总结 一句话总结:当试图获取一个不可达变量时,类会自动调用__get.同样的,当试图设置一个不可达变量时,类会自动调用__set.在网站中, ...
- 在Sql中将 varchar 值 '1,2,3,4,5,6' 转换成数据类型 int
--问题:将aa转换为Int类型失败 string aa="3,5,11,56,88,45,23"; select * from ERPBuMen where ID in(aa) ...
- ArcGIS小技巧——多图层情况下交互显示效果
在使用ArcMap处理数据的过程中,通常需要对比不同图层之间的差异.或者查看影像配准情况,这时我通常会怀念ENVI中的强大的拉幕显示.闪烁.亮度和透明度显示工具...... 直到有一天,闲着没事干捣鼓 ...
- 【习题 7-6 UVA - 12113】Overlapping Squares
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴 ...
- [bzoj1269]文本编辑器editor [bzoj1500]维修数列
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2540 Solved: 923 [Submit ...
- 深入理解HTTP协议及原理分析之缓存(3种缓存机制)
3.2 缓存的实现原理 3.2.1什么是Web缓存 WEB缓存(cache)位于Web服务器和客户端之间. 缓存会根据请求保存输出内容的副本,例如html页面,图片,文件,当下一个请求来到的时候:如果 ...
- 手撕面试题ThreadLocal!!!
说明 面试官:讲讲你对ThreadLocal的一些理解. 那么我们该怎么回答呢????你也可以思考下,下面看看零度的思考: ThreadLocal用在什么地方? ThreadLocal一些细节! Th ...
- CMake - SWIG - 移植动态库
CMake - SWIG 最后更新日期:2014-04-25 bykagula 阅读前提:<CMake入门(二)>.<同Java的混合编程-SWIG>.Linux的基本操作.j ...
- 微信小程序踩坑- tabBar.list[3].selectedIconPath 大小超过 40kb
重新启动微信小程序编辑器的时候遇到了这样的一个问题: tabBar.list[3].selectedIconPath 大小超过 40kb 微信小程序开发的过程之中总会出现这样或者那样的错误,需要我们耐 ...
- REGEXP_LIKE,REGEXP_INSTR,REGEXP_SUBSTR,REGEXP_REPLACE
参考: http://www.cnblogs.com/scottckt/archive/2012/10/11/2719562.html http://www.jb51.net/article/3842 ...