grep命令最经常使用的功能总结

1. grep最简单的用法,匹配一个词:grep word filename

2. 能够从多个文件里匹配:grep word filename1 filenam2 filename3

3. 能够使用正則表達式匹配:grep -E pattern f1 f2 f3...

4. 能够使用-o仅仅打印匹配的字符,例如以下所看到的:

[email protected]:command$ echo this is a line. | grep -E -o "[a-z]*\."
line.

5. 打印除匹配行之外的其它行,使用-v

[email protected]:command$ echo -e "1\n2\n3\n4" | grep -v -E "[1-2]"
3
4

6. 统计匹配字符串的行数。使用-c

[email protected]:command$ echo -e "1111\n2222" | grep -E "[1-2]" -c
2

7. 假设我们统计字符串模式匹配的次数。能够结合-o和-c。例如以下:

[email protected]:command$ echo -e "1111\n2222" | grep -o -E "[1-2]"  | wc -l
8

8. 假设须要显示行号,能够打开-n,例如以下:

[email protected]:command$ echo -e "1111\n2222\n33333\n44444" | grep -n -E "3"
3:33333

9. -b选项能够打印出匹配的字符串想对于其所在的行起始位置的偏移量(从0開始)。通常配合-o使用,例如以下:

[email protected]:command$ echo "0123456789" | grep -b -o 4
4:4

10. 当字符串在多个文件里匹配时。-l选项将仅仅打印文件名称

11. -L与-l相对。仅仅打印不匹配的文件名称

[email protected]:command$ cat test1.txt
linux
is
fun
[email protected]:command$ cat test2.txt
a
very
popular
os,
linux
[email protected]:command$ cat test3.txt
what
the
fxxk
[email protected]:command$ grep -l linux test1.txt test2.txt test3.txt
test1.txt
test2.txt
[email protected]:command$ grep -L linux test1.txt test2.txt test3.txt
test3.txt

12. 打开递归搜索功能

[email protected]:command$ grep -n -R linux .
./test2.txt:5:linux
./test1.txt:1:linux

13. 忽略大写和小写:-i

[email protected]:command$ echo "HELLO WORLD" | grep -i "hello"
HELLO WORLD

14. 匹配多个字符串模式

[email protected]:command$ echo "This is a line." | grep -e "This" -e "is" -e "line" -o
This
is
line

15. 用单独的文件提供匹配样式,每一个匹配的样式作为一行,例如以下例所看到的:

[email protected]:command$ cat pattern.txt
1$
2
3
[email protected]:command$ cat num.txt
1
2
3
4
5
6
7
8
9
10
[email protected]:command$ grep -f pattern.txt num.txt
1
2
3

16. 打印匹配行上下文信息,使用 -A n打印匹配行及其后n行信息。使用-B n打印匹配行及其前n行信息。使用 -C n。打印匹配行及其前后n行信息。假设有多重匹配,将使用--隔离。

示比例如以下:

[email protected]:command$ seq 1 10 | grep 5 -A 3
5
6
7
8
[email protected]:command$ seq 1 10 | grep 5 -B 3
2
3
4
5
[email protected]:command$ seq 1 10 | grep 5 -C 3
2
3
4
5
6
7
8
[email protected]: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命令执行目的不过执行一个条件測试。通常在脚本中使用。通过检查其返回值进行下一步操作。示比例如以下:

[email protected]:command$ cat tmp.txt
hello
world
[email protected]: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
[email protected]:command$ ./tmp.csh hello tmp.txt
hello exist in tmp.txt

18. -Z选项在输出匹配文件名称时将以/0结尾配合xargs -0能够发挥非常多作用,比如删除匹配某个模式的文件例如以下:

[email protected]: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
[email protected]:command$ cat test1.txt
linux
is
fun
[email protected]:command$ cat test2.txt
a
very
popular
os,
linux
[email protected]:command$ grep "linux" * -lZ | xargs -0 rm
[email protected]: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中读取须要排除的文件列表

[email protected]:test$ ls
dir1  dir2  exclude.config  test1.txt  test2.doc  test3.word
[email protected]:test$ cat test1.txt
linux
is
fun
[email protected]:test$ cat test2.doc
wonderful
os,
linux
[email protected]:test$ cat test3.word
wonderful
os,
linux
[email protected]:test$ ls dir1/
test1.txt  test2.doc  test3.word
[email protected]:test$ ls dir2/
test1.txt  test2.doc  test3.word
[email protected]:test$ cat exclude.config
*.txt
[email protected]: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
[email protected]: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
[email protected]:test$ grep "linux" -R -n . --exclude *.txt --eclude *.doc
grep: unrecognized option ‘--eclude‘
Usage: grep [OPTION]... PATTERN [FILE]...
Try ‘grep --help‘ for more information.
[email protected]:test$ grep "linux" -R -n . --exclude *.txt --exclude *.doc
./test3.word:3:linux
./dir2/test3.word:3:linux
./dir1/test3.word:3:linux
[email protected]: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
[email protected]:test$ grep "linux" -R -n . --exclude-dir dir1 --exclude-dir dir2
./test2.doc:3:linux
./test3.word:3:linux
./test1.txt:1:linux
[email protected]: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经常使用的选项。

注意:转载请注明出处。

时间: 2024-08-08 01:00:06

grep命令最经常使用的功能总结的相关文章

grep命令的or,and,not操作的例子

在Linux的grep命令中如何使用OR,AND,NOT操作符呢? 其实,在grep命令中,有OR和NOT操作符的等价选项,但是并没有grep AND这种操作符.不过呢,可以使用patterns来模拟AND操作的.下面会举一些例子来说明在Linux的grep命令中如何使用OR,AND,NOT. 在下面的例子中,会用到这个employee.txt文件,如下: $ cat employee.txt 100 Thomas Manager Sales $5,000 200 Jason Developer

grep命令最常用的功能总结

1. grep最简单的使用方法,匹配一个词:grep word filename 2. 可以从多个文件中匹配:grep word filename1 filenam2 filename3 3. 可以使用正则表达式匹配:grep -E pattern f1 f2 f3... 4. 可以使用-o只打印匹配的字符,如下所示: [email protected]:command$ echo this is a line. | grep -E -o "[a-z]*\." line. 5. 打印除

Linux     grep命令,正则表达式

grep命令及正则表达式 grep命令 grep , egrep , fgrep grep,sed,awk 文本处理三剑客 grep: Global search REgular expression and Print out the line;全面查找正则表达式并将匹配到的行显示出来; 正则表达式 正则表达式主要应用对象是文本,因此它在各种文本编辑器场合都有应用;许多程序设计语言都支持利用正则表达式进行字符串操作; 主流的正则引擎又分为三类:DFA;传统型NFA;POSIX NFA; DFA

正则表达式和grep命令的用法

正则表达式和grep命令的用法: 一.正则表达式: 正则表达式(也称为regular Expression,简称RE)就是由普通字符(例如字符a到z)以及特殊字符(称之为元字符)组成的文字模式. 该模式描述在查找文字主体时待匹配的一个或多个字符串. 正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配.简单的说,正则表示式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表示通过一些特殊符号的辅助,可以让使用者轻易的达到搜寻/删除/取代某特定字符串的处理程序.vim.gr

【转】每天一个linux命令(39):grep 命令

原文网址:http://www.cnblogs.com/peida/archive/2012/12/17/2821195.html Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板.如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名.搜索的

grep命令详解

grep命令是linux下的行过滤工具,其参数繁多,下面就一一介绍个个参数的作用,希望对大家有所帮助.grep -- print lines matching a pattern (将符合样式的该行列出)◎语法: grep [options] PATTERN [FILE...] grep用以在file内文中比对相对应的部分,或是当没有指定档案时, 由标准输入中去比对. 在预设的情况下,grep会将符合样式的那一行列出.此外,还有两个程序是grep的变化型,egrep及fgrep. 其中egrep

grep命令以及正则表达式,算数运算.

最近一段时间一直在学习算术运算和正则表达式以及条件判断,连续看了四五遍,基本概念已经能够搞清楚了,实际操作中容易把正则表达式和算术运算以及条件判断混淆.看来还是要勤加联系.今天终于是有勇气来总结这两周的学习.在这之前,因为在练习这些逻辑概念性的知识经常会用到grep命令.首先介绍一下grep. 格式:grep [options] 'PATTERN' file,... grep: 文本搜索工具,根据用户指定的文本模式对目标文件进行逐行搜索,显示文件中能够被模式所匹配到的行. 模式PATTERN:指

[Shell]grep命令

我是好文章的搬运工,原文来自ChinaUnix,博主scq2099yt,地址:http://blog.chinaunix.net/uid-22312037-id-4217835.html 一.基本用法        grep是linux中很常用的一个命令,主要功能就是进行字符串数据的对比,能使用正则表达式搜索文本,并将符合用户需求的字符串打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户.grep在数据中查找出一

liunx 的 grep命令(转载)

简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. grep常用用法 [[email protected] ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename 选项与参数: -a :将 binary 文件以 text 文件的方式搜寻数据 -c :计算找到