`
xitong
  • 浏览: 6177093 次
文章分类
社区版块
存档分类
最新评论

Linux/Unix下grep命令使用的几个例子[grep Examples]

 
阅读更多

grep 语法

grep 'word' filename
grep 'string1 string2'  filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep --color 'data' fileName

基本的用法

在某个文件里搜索error字符串
$ grep "error" log.txt


忽略大小写搜索(-i)

$ grep -i "ErroR" log.txt


所有子目录下的搜索(-r)

$ grep -r "exception" log.txt


全字匹配搜索(-w)

如果你搜索boo,查询结果可能包含fooboo,boo123, booooom,等等,可以使用-w来限定全字匹配
$ grep -w "boo" /path/to/file


全字匹配搜索两个不同单词

$ grep -w 'word1|word2' /path/to/file


统计字符串出现的次数(-c)

$ grep -c 'word' /path/to/file

另外加-n的话, 会在结果中,列出匹配字符串的序列号,并且会列出内容
$ grep -n 'word' /path/to/file


列出“不”包含字符串的行(-v)

$ grep -v bar /path/to/file


只列出文件名(-l)

$ grep -l 'main' *.pls


高亮显示(--color)

$ grep --color oracle /etc/passwd


UNIX / Linux pipes + grep

ls -l | grep -i xyz

ls 列出当前目录下的文件和文件夹,| 是管道传递给后面的一个程序,grep再是进行模式匹配

例如:ls *.pls | grep -i --color "MM"

========EOF=========

转载请注明出处:http://blog.csdn.net/pan_tian/article/details/7685815


分享到:
评论

相关推荐

    linux全志R16的linux系统编译的资料_20170502_1655.7z

    config/autoconf.h KCONFIG_TRISTATE=/home/wwt/linux_r16/lichee/out/sun8iw5p1/linux/common/buildroot/build/buildroot-config/tristate.config BUILDROOT_CONFIG=/home/wwt/linux_r16/lichee/out/sun8iw5p1/...

    最全的oracle常用命令大全.txt

    如果这命令不行,找它UNIX的进程数 SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr; 说明:21是某个连接的sid数 然后用 kill 命令杀此进程号。 五、SQL*PLUS...

    Prentice.Hall.PTR.UNIX.Shells.by.Example.4th.Edition

    She also systematically introduces awk, sed, and grep for both UNIX and GNU/Linux . . . making this the only shell programming book you’ll ever need! New in this edition: Comprehensive coverage of...

    Linux高级bash编程

    几个等效命令test,/usr/bin/test,[],和/usr/bin/[ 7-3. 算数测试使用(( )) 7-4. test死的链接文件 7-5. 数字和字符串比较 7-6. 测试字符串是否为null 7-7. zmore 8-1. 最大公约数 8-2. 使用算术操作符 8-3. 使用&&...

    Advanced Bash-Scripting Guide <>

    几个等效命令test,/usr/bin/test,[],和/usr/bin/[ 7-3. 算数测试使用(( )) 7-4. test 死的链接文件 7-5. 数字和字符串比较 7-6. 测试字符串是否为null 7-7. zmore 8-1. 最大公约数 8-2. 使用算术操作符 8-3. 使用&&...

    mpl:用于并行ML的MaPLe编译器

    码头工人在Docker上尝试MPL: $ docker pull shwestrick/mpl$ docker run -it shwestrick/mpl /bin/bash...# examples/bin/primes @mpl procs 4 --构建和安装(从源代码)要求MPL仅在具有x86-64Linux上进行了测试。...

    cardinal:语法突出显示要打印到终端的具有ANSI颜色JavaScript代码

    红衣主教 car·di·nal (carndi-l,kärdnl) -北美大嘴朱雀,雄性有鲜红色的羽毛。 特征 用ANSI颜色突出显示JavaScript代码以改善终端输出 主题支持,请参阅 可选地打印行号 ...cat file.js | grep

    sed_awk.chm

    The pattern-matching examples in this chapter largely rely on grep and egrep. Chapter 4, Writing sed Scripts, begins a three-chapter section on sed. This chapter covers the basic elements of writing...

    Turbo C 2.00[DISK]

    WELCOME TO TURBO C 2.0 ---------------------- This README file contains important, last minute information about Turbo C 2.0.....DOC file on the COMMAND LINE/UTILITIES disk also answers many common ...

    Turbo C 2.01[DISK]

    WELCOME TO TURBO C 2.01 ----------------------- This README file contains important, last minute information about Turbo C 2.01.....DOC file on the COMPILER/UTILITIES disk also answers many common ...

    BSD HACKS -- 100个业界最尖端的技巧和工具

    How to Use this Book How This Book Is Organized Conventions Used in This Book Using Code Examples We'd Like to Hear from You Chapter 1. Customizing the User Environment ...

Global site tag (gtag.js) - Google Analytics