this is a words file.
words words to be
, , , , , , , , , ,
beginning linux programming 4th edition this is a line containing pattern
,.<>?;';;;' [] {= = \ \ \| -__!@#$%^&*() !@#$$%%^&*(()*&%@(#$%)) www.regexper.com
www.google.com
www.baidu.com
www.redhat.com

我们的测试文件名字叫 n,如上所示,共13行。

grep按行检索,按行输出。

1, 搜索特定模式的行

 [lizhen@dhcp-- shell]$ grep words n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$

2,单个grep命令可以对多个文件进行检索

[lizhen@dhcp-- shell]$ grep words n n1 n2
n:this is a words file.
n:words words to be
n1:this is a words file.
n1:words words to be
n2:this is a words file.
n2:words words to be
[lizhen@dhcp-- shell]$

3,使用正则表达式,添加-E选项,或者直接egrep   (在terminal下可以看到这些被匹配的部分 是被红色 特殊显示的,这里显示的是被匹配到的 行)

[lizhen@dhcp-- shell]$ egrep "[a-o]+" n
this is a words file.
words words to be
beginning linux programming 4th edition
this is a line containing pattern
www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$

4,只输出文件中匹配到的文本部分呢,使用-o

[lizhen@dhcp-- shell]$ grep words n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$ grep words n -o
words
words
words
[lizhen@dhcp-- shell]$

5,打印除包含match_pattern行之外的所有行,使用-v

[lizhen@dhcp-- shell]$ grep words n -v
, , , , , , , , , ,
beginning linux programming 4th edition this is a line containing pattern
,.<>?;';;;' [] {= = \ \ \| -__!@#$%^&*() !@#$$%%^&*(()*&%@(#$%)) www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$

6,统计文件包含匹配字符串的行数,使用-c   (-c只统计匹配到的行数,并不会统计匹配到的次数)

[lizhen@dhcp-- shell]$ grep words n -c

[lizhen@dhcp-- shell]$ grep words n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$

7,统计匹配到的字符串的数量,使用-o    |    wc -l

[lizhen@dhcp-- shell]$ grep -o words n | wc -l

[lizhen@dhcp-- shell]$ grep words n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$

8,打印出包含匹配字符串的行号,使用-n

[lizhen@dhcp-128-93 shell]$ grep w -n n n1
n:1:this is a words file.
n:2:words words to be
n:10:www.regexper.com
n:11:www.google.com
n:12:www.baidu.com
n:13:www.redhat.com
n1:1:this is a words file.
n1:2:words words to be
n1:10:www.regexper.com
n1:11:www.google.com
n1:12:www.baidu.com
n1:13:www.redhat.com
[lizhen@dhcp-128-93 shell]$

9 打印模式匹配所位于的字符或字节偏移,使用-b  -o

[lizhen@dhcp-- shell]$ grep words n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$ grep words -b -o n
:words
:words
:words
[lizhen@dhcp-- shell]$

10,搜索多个文件并找出文本位于哪一个文件中,使用-l

[lizhen@dhcp-- shell]$ grep words n n1
n:this is a words file.
n:words words to be
n1:this is a words file.
n1:words words to be
[lizhen@dhcp-- shell]$ grep words -l n n1
n
n1
[lizhen@dhcp-- shell]$

使用-L  大写的L字符,取相反的结果

[lizhen@dhcp-- shell]$ grep words n n1
n:this is a words file.
n:words words to be
n1:this is a words file.
n1:words words to be
[lizhen@dhcp-- shell]$ grep words -l n n1
n
n1
[lizhen@dhcp-- shell]$ grep words -L n n1
[lizhen@dhcp-- shell]$

11,递归搜索文件,使用-R -n  (-n选项表示显示所在文件名:行号)

[lizhen@dhcp-- shell]$ grep words . -R -n
./n::this is a words file.
./n::words words to be
./n1::this is a words file.
./n1::words words to be
./n2::this is a words file.
./n2::words words to be
[lizhen@dhcp-- shell]$

12,忽略样式中的大小写,使用-i

[lizhen@dhcp-- shell]$ grep WORDS -i n
this is a words file.
words words to be
[lizhen@dhcp-- shell]$

13,使用grep匹配多个样式,使用-e

[lizhen@dhcp-128-93 shell]$ grep -e words  -e www -o n
words
words
words
www
www
www
www
[lizhen@dhcp-128-93 shell]$

14,使用样式文件,利用grep逐行读取样式文件,grep会将匹配到的行输出

[lizhen@dhcp-- shell]$ grep -f f n
this is a words file.
words words to be
, , , , , , , , , , www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$

15,在grep搜索中指定或排除文件

# grep "main()" . -r --include *.{c,cpp}

#grep "main()" . -r --exclude "readme"

16,grep 的静默输出,使用-q

#########################################################################
# File Name: begin.sh
# Author: lizhen
# mail: lizhen_ok@.com
# Created Time: Wed May :: PM CST
#########################################################################
#!/bin/bash
if [ $# -ne ]
then
echo "usage: $0 match_text filename"
exit
fi match_text=$
filename=$
grep -q "$match_text" $filename if [ $? -eq ]
then
echo "The text exists in the file"
else
echo "text does not exist in the file"
fi echo "done!"
[lizhen@dhcp-- shell]$ ./begin.sh words n
The text exists in the file
done!
[lizhen@dhcp-- shell]$

17,打印匹配行之前或之后的行,使用-B,-A,-C选项

[lizhen@dhcp-- shell]$ grep www -B  n
this is a line containing pattern
,.<>?;';;;' [] {= = \ \ \| -__!@#$%^&*() !@#$$%%^&*(()*&%@(#$%)) www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$ grep www -B n www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$ grep www n
www.regexper.com
www.google.com
www.baidu.com
www.redhat.com
[lizhen@dhcp-- shell]$
[lizhen@dhcp-- shell]$ grep words -A   n
this is a words file.
words words to be
, , , , , , , , , ,
[lizhen@dhcp-- shell]$
[lizhen@dhcp-- shell]$ grep  n

[lizhen@dhcp-- shell]$ grep  n -C
beginning linux programming 4th edition [lizhen@dhcp-- shell]$
												

文本处理grep命令的更多相关文章

  1. Linux命令-文件文本操作grep

    文件文本操作 grep 在文件中查找符合正则表达式条件的文本行 cut 截取文件中的特定字段 paste 附加字段 tr 字符转换或压缩 sort 调整文本行的顺序,使其符合特定准则 uniq 找出重 ...

  2. 【OS_Linux】三大文本处理工具之grep命令

    grep(global search regular expression(RE) and print out the line,整行搜索并打印匹配成功的行 语法:grep  [选项]   搜索词  ...

  3. linux grep命令

    linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressio ...

  4. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  5. grep 命令详解

    [root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename 选项与参数: -a :将 binary 文件以 text 文件的方式搜寻数据 - ...

  6. grep 命令操作

    linux grep命令 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...

  7. Linux命令(23)grep命令的使用

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  8. 每天一个linux命令(39):grep 命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  9. 由一条Linux的grep命令说起

    今天在开发的时候,看到同事使用了这样的一条linux命令 grep 'class YourClass' -rwi * |grep -v svn 想到了 grep命令的,几个参数. -r 明确要求搜索子 ...

随机推荐

  1. [Rails学习之路]初识Ruby(一)

    Ruby是一门动态的.强类型的.纯面向对象的编程语言.它和Python非常相似,但比Python面向对象更加彻底.使用更加灵活.语法更加复杂.也更为有趣. 抛开做事情到底应该有多少种方法这个问题,我相 ...

  2. iOS解析新浪微博的@##以及URL链接并展示

    最近在做一个跟微博相关的应用.其中涉及到了对微博中@.##以及URL链接的解析与展示.分享一下个人处理的方式,希望对需要的人有所帮助. 最终的展现效果: 首先,第一步是你得从纯文本中找到它们.毫无疑问 ...

  3. Aizu 2300 Calender Colors(暴力)

    状压以后,直接暴力枚举,2^20约等于1e6,而且满足bitcount = m的状态很少. #include<bits/stdc++.h> using namespace std; +; ...

  4. gitlab系列详解

    虚拟机的安装1.安装virtualboxhttps://www.virtualbox.org/2.安装centos6.63.配置网络右键-->网络-->网卡2-->host-only ...

  5. 第十四篇、OC_新闻查看器

    PageTitleView: #import <UIKit/UIKit.h> @class GFBPageTitleView; @protocol GFBPageTitleViewDele ...

  6. 学习 KMP 算法

    KMP 算法是用来处理字符串匹配问题的.也就是给你两个字符串,你需要回答:B 串是否是 A 串的子串(或 B 串在 A 串中出现的位置).比如,字符串 A = “ i am student ”, 字符 ...

  7. python虚拟环境 virtualenv工具

    为了隔离各类环境,保证环境间不冲突,python中存在虚拟环境,可以在一个文件夹里生成相应的环境,防止与python自带环境冲突 首先我们下载virtualenv,若你未安装python,应到pyth ...

  8. PHP静态文件缓存

    ob_start(); 2 echo 'aaa'; 3 $string = ob_get_contents(); 4 file_put_contents('a.html', $string); 5 o ...

  9. Flask学习笔记:数据库ORM操作MySQL+pymysql/mysql-python+SQLAlchemy/Flask-SQLAlchemy

    Python中使用sqlalchemy插件可以实现ORM(Object Relationship Mapping,模型关系映射)框架,而Flask中的flask-sqlalchemy其实就是在sqla ...

  10. 【luminate primordial】苏州之行

    测试了reader 07版 更主要的是第一次坐了高铁 还不错 路上看到下雨的时候都是水顺着玻璃平着流 好厉害的样子 6个人去的6个人回 今儿开会 老板不太满意 小随意 对我来说,收获感觉还是不小的,使 ...