grep命令最经常使用的功能总结
1. grep最简单的用法,匹配一个词:grep word filename
2. 能够从多个文件里匹配:grep word filename1 filenam2 filename3
3. 能够使用正則表達式匹配:grep -E pattern f1 f2 f3...
4. 能够使用-o仅仅打印匹配的字符,例如以下所看到的:
lichao@ubuntu:command$ echo this is a line. | grep -E -o "[a-z]*\."
line.
5. 打印除匹配行之外的其它行,使用-v
lichao@ubuntu:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"
3
4
6. 统计匹配字符串的行数。使用-c
lichao@ubuntu:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c
2
7. 假设我们统计字符串模式匹配的次数。能够结合-o和-c。例如以下:
lichao@ubuntu:command$ echo -e "1111\n2222" | grep -o -E "[1-2]" | wc -l
8
8. 假设须要显示行号,能够打开-n,例如以下:
lichao@ubuntu:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"
3:33333
9. -b选项能够打印出匹配的字符串想对于其所在的行起始位置的偏移量(从0開始)。通常配合-o使用,例如以下:
lichao@ubuntu:command$ echo "0123456789" | grep -b -o 4
4:4
10. 当字符串在多个文件里匹配时。-l选项将仅仅打印文件名称
11. -L与-l相对。仅仅打印不匹配的文件名称
lichao@ubuntu:command$ cat test1.txt
linux
is
fun
lichao@ubuntu:command$ cat test2.txt
a
very
popular
os,
linux
lichao@ubuntu:command$ cat test3.txt
what
the
fxxk
lichao@ubuntu:command$ grep -l linux test1.txt test2.txt test3.txt
test1.txt
test2.txt
lichao@ubuntu:command$ grep -L linux test1.txt test2.txt test3.txt
test3.txt
12. 打开递归搜索功能
lichao@ubuntu:command$ grep -n -R linux .
./test2.txt:5:linux
./test1.txt:1:linux
13. 忽略大写和小写:-i
lichao@ubuntu:command$ echo "HELLO WORLD" | grep -i "hello"
HELLO WORLD
14. 匹配多个字符串模式
lichao@ubuntu:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o
This
is
line
15. 用单独的文件提供匹配样式,每一个匹配的样式作为一行,例如以下例所看到的:
lichao@ubuntu:command$ cat pattern.txt
1$
2
3
lichao@ubuntu:command$ cat num.txt
1
2
3
4
5
6
7
8
9
10
lichao@ubuntu:command$ grep -f pattern.txt num.txt
1
2
3
16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息。使用-B n打印匹配行及其前n行信息。使用 -C n。打印匹配行及其前后n行信息。假设有多重匹配,将使用--隔离。
示比例如以下:
lichao@ubuntu:command$ seq 1 10 | grep 5 -A 3
5
6
7
8
lichao@ubuntu:command$ seq 1 10 | grep 5 -B 3
2
3
4
5
lichao@ubuntu:command$ seq 1 10 | grep 5 -C 3
2
3
4
5
6
7
8
lichao@ubuntu:command$ echo -e "a\nb\nc\nd\na\nb\nc\nd\n" | grep a -A 2
a
b
c
--
a
b
c
17. 使用-q进入静默模式,该模式下。grep命令执行目的不过执行一个条件測试。通常在脚本中使用。通过检查其返回值进行下一步操作。示比例如以下:
lichao@ubuntu:command$ cat tmp.txt
hello
world
lichao@ubuntu:command$ cat tmp.csh
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Usage: $0 match_pattern file_name"
exit
fi
match=$1
file=$2
grep -q $match $file
if [ $? -ne 0 ]; then
echo "$match not exist in $file"
else
echo "$match exist in $file"
fi
lichao@ubuntu:command$ ./tmp.csh hello tmp.txt
hello exist in tmp.txt
18. -Z选项在输出匹配文件名称时将以/0结尾配合xargs -0能够发挥非常多作用,比如删除匹配某个模式的文件例如以下:
lichao@ubuntu:command$ ls -llrt
total 28
-rw-rw-r-- 1 lichao lichao 13 Nov 1 20:38 test1.txt
-rw-rw-r-- 1 lichao lichao 27 Nov 1 20:39 test2.txt
-rw-rw-r-- 1 lichao lichao 14 Nov 1 20:39 test3.txt
-rw-rw-r-- 1 lichao lichao 21 Nov 1 20:45 num.txt
-rw-rw-r-- 1 lichao lichao 7 Nov 1 20:45 pattern.txt
-rw-rw-r-- 1 lichao lichao 12 Nov 1 21:25 tmp.txt
-rwxr-xr-x 1 lichao lichao 217 Nov 1 21:27 tmp.csh
lichao@ubuntu:command$ cat test1.txt
linux
is
fun
lichao@ubuntu:command$ cat test2.txt
a
very
popular
os,
linux
lichao@ubuntu:command$ grep "linux" * -lZ | xargs -0 rm
lichao@ubuntu:command$ ls
num.txt pattern.txt test3.txt tmp.csh tmp.txt
以上命令将包括linux字符串的test1.txt和test2.txt删除。
19. 排除/包含文件或者文件夹:1)--include *{.c,.cpp} 仅仅在文件夹中搜索.c和.cpp文件;2)--exclude "README" 排除全部README文件 3) --include-dir 仅在某些文件夹中搜索 4) --exclude-dir 排除某些文件夹 5) --exclude-from FILE 从文件FILE中读取须要排除的文件列表
lichao@ubuntu:test$ ls
dir1 dir2 exclude.config test1.txt test2.doc test3.word
lichao@ubuntu:test$ cat test1.txt
linux
is
fun
lichao@ubuntu:test$ cat test2.doc
wonderful
os,
linux
lichao@ubuntu:test$ cat test3.word
wonderful
os,
linux
lichao@ubuntu:test$ ls dir1/
test1.txt test2.doc test3.word
lichao@ubuntu:test$ ls dir2/
test1.txt test2.doc test3.word
lichao@ubuntu:test$ cat exclude.config
*.txt
lichao@ubuntu:test$ grep "linux" -R -n .
./test2.doc:3:linux
./test3.word:3:linux
./test1.txt:1:linux
./dir2/test2.doc:3:linux
./dir2/test3.word:3:linux
./dir2/test1.txt:1:linux
./dir1/test2.doc:3:linux
./dir1/test3.word:3:linux
./dir1/test1.txt:1:linux
lichao@ubuntu:test$ grep "linux" -R -n . --include *.txt --include *.doc
./test2.doc:3:linux
./test1.txt:1:linux
./dir2/test2.doc:3:linux
./dir2/test1.txt:1:linux
./dir1/test2.doc:3:linux
./dir1/test1.txt:1:linux
lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc
grep: unrecognized option '--eclude'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
lichao@ubuntu:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc
./test3.word:3:linux
./dir2/test3.word:3:linux
./dir1/test3.word:3:linux
lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1
./test2.doc:3:linux
./test3.word:3:linux
./test1.txt:1:linux
./dir2/test2.doc:3:linux
./dir2/test3.word:3:linux
./dir2/test1.txt:1:linux
lichao@ubuntu:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2
./test2.doc:3:linux
./test3.word:3:linux
./test1.txt:1:linux
lichao@ubuntu:test$ grep "linux" -R -n . --exclude-from exclude.config
./test2.doc:3:linux
./test3.word:3:linux
./dir2/test2.doc:3:linux
./dir2/test3.word:3:linux
./dir1/test2.doc:3:linux
./dir1/test3.word:3:linux
已上即为grep经常使用的选项。
注意:转载请注明出处。
grep命令最经常使用的功能总结的更多相关文章
- grep命令的or,and,not操作的例子
在Linux的grep命令中如何使用OR,AND,NOT操作符呢? 其实,在grep命令中,有OR和NOT操作符的等价选项,但是并没有grep AND这种操作符.不过呢,可以使用patterns来模拟 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- grep 命令操作
linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...
- 每天一个linux命令(39):grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- 由一条Linux的grep命令说起
今天在开发的时候,看到同事使用了这样的一条linux命令 grep 'class YourClass' -rwi * |grep -v svn 想到了 grep命令的,几个参数. -r 明确要求搜索子 ...
- liunx 的 grep命令(转载)
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- linux命令学习(1):grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- Linux之grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- linux grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
随机推荐
- root连接ubuntu18.04“拒绝访问”的解决方法
1.设置root账户 sudo passwd root 2.ssh远程登陆拒绝访问:修改SSH配置文件 sudo vim /etc/ssh/sshd_config 找到并用#注释掉这行:PermitR ...
- 条款27:尽量少做转型动作(Minimize casting)
NOTE : 1.如果可以,尽量避免转型,特别是在注重效率的代码中避免dynamic_casts. 如果有个设计需要转型动作,试着发展无需转型的替代设计. 2.如果转型是必须要的,试着将它隐藏于某个函 ...
- (十九)python 3 内嵌函数和闭包
内嵌函数:函数里又嵌套一个函数 def fun1(): print('fun1()在被调用') def fun2(): print('fun2()在被调用') fun2() 闭包: 闭包是函数里面嵌套 ...
- python中的多任务
多任务 什么是任务 一个电脑运行这的软件 什么是多任务 电脑同时运行着的多个软件 多任务原理 时间片的轮转 并行与并发 并发:假的多任务,多个任务共用一个核 并行:正的多任务,一个核处理一个程序 生理 ...
- 72.spring boot讨论群【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...
- hdu1856 选出更多的孩子
题目大意: 老师选取2个学生对应的号码,这两人视作朋友,同时朋友的朋友也可以看成自己的朋友. 最后老师选出一个人数最多的朋友圈. 这里学生的人数不大于10^7,所以操作时需要极为注意,操作步数能省则省 ...
- 骑士精神 (codevs 2449)
题目描述 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标 ...
- Codevs 3556 科技庄园==洛谷 P2760
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Life是codevs的用户,他是一个道德极高的用户,他积极贯彻党的十八大精神, ...
- Http、TCP/IP、Socket的区别
网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...
- 视图中使用foreach报错问题
问题情境:thinkphp3.2版本,使用四层<foreach></foreach>循环变量时,报错以下错误: syntax error, unexpected 'endfor ...