首页 > 系统相关 >Linux文本处理三剑客之grep

Linux文本处理三剑客之grep

时间:2023-08-20 17:35:42浏览次数:64  
标签:boy grep -- lines 文本处理 day match 三剑客

相信大家对于grep都不陌生,或多或少都用过。

但大部分人可能都只用过最基本的字符匹配,而稍微复杂一点的用法没有使用过。

我们不追求过于复杂的参数用法,而是要了解grep还能干什么,有什么我平常没有用到的功能,从而能够提升我的工作效率。

比如我自己,很长一段时间,我都只会用:

grep -Enr 'xxx' file

如果工作中经常需要用到文本搜索,那么掌握grep更多用法就很有必要了。

命令格式

命令语法

grep [OPTION]... PATTERNS [FILE]...

命令选项

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data
      --no-ignore-case      do not ignore case distinctions (default)
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM selected lines
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print file name with output lines
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only nonempty parts of lines that match
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --include=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files that match GLOB
      --exclude-from=FILE   skip files that match any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)

关于正则表达式的模式选择(-E, -F, -G, -P)可参考知乎文章:

https://zhuanlan.zhihu.com/p/435815082

几个派系有所区别,我本人习惯用 -E (extended regular expressions)。

常用参数

  -E, --extended-regexp     选择正则表达式的模式(扩展模式)
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         单词匹配
  -v, --invert-match        反转模式(输出不含有目标字符的文本)
  -n, --line-number         打印匹配的行号
  -r, --recursive           递归搜索(搜索对象是目录)
  -c, --count               仅仅打印匹配的个数

案例

创建测试文件grep.txt, 内容如下:

$ cat grep.txt
Today is a good day, a sunny day, a wonderful day, a important day.
I am a boy, a good boy, a lovely boy.
I like reading, sports, and coding.
Enjoy coding.

基本搜索

搜索文本中含有字母'a'的所有行,

-E: 扩展模式

-n: 输出行号

可以看到其实reading/and中的a也配匹配到了。

grep -En "a" grep.txt
1:Today is a good day, a sunny day, a wonderful day, a important day.
2:I am a boy, a good boy, a lovely boy.
3:I like reading, sports, and coding.

现在限制为单词匹配,

加上参数 -w.

$ grep -Ewn "a" grep.txt
1:Today is a good day, a sunny day, a wonderful day, a important day.
2:I am a boy, a good boy, a lovely boy.

只显示匹配行的行数。

(注意不是a的个数,而是含有目标字符的行数)

$ grep -Ewnc "a" grep.txt
2

进阶搜索

匹配多个模式

也就是同时匹配多个文本或者字符。

同时匹配含有a或者boy的行;

$ grep -Ewn "a|boy" grep.txt
1:Today is a good day, a sunny day, a wonderful day, a important day.
2:I am a boy, a good boy, a lovely boy.

那如果我要匹配同时含有a和boy的行呢?

.表示匹配任意字符;
*表示任意个;

意思就是说a和boy中可以有任意字符;

$ grep -Ewn "a.*boy" grep.txt
2:I am a boy, a good boy, a lovely boy.

更多参数用法

比如有时候我比较关注发生匹配时前面几行或者后面几行,就可以使用

-A n; 打印模式匹配之后的n行;

-B n; 打印模式匹配之前的n行;

-C n; 打印模式匹配周围的n行;

$ grep -A 1 -Ewn "a.*boy" grep.txt
2:I am a boy, a good boy, a lovely boy.
3-I like reading, sports, and coding.

善用正则表达式组合

如果熟悉正则表达式,可以更加灵活的搜索自己想要的文本。

可参考正则表达式大全:https://www.cnblogs.com/fozero/p/7868687.html

比如搜索数字,字母;

比如匹配手机号,身份证号;

各种pattern都可以通过正则表达式达到要求。

标签:boy,grep,--,lines,文本处理,day,match,三剑客
From: https://www.cnblogs.com/bailiji/p/17644284.html

相关文章

  • 软件测试|Linux三剑客之awk命令详解
    简介awk是一种强大的文本处理工具,在Unix和类Unix系统中广泛使用。它允许您在文本文件中进行复杂的数据处理和格式化输出。awk的名字是根据它的三位创始人Aho、Weinberger和Kernighan姓氏的首字母命名的。本文将详细介绍awk命令的基本用法和一些常见的用例。awk基本语法aw......
  • 软件测试|Linux三剑客之grep命令详解
    简介grep是一款在Linux和类Unix系统中广泛使用的文本搜索工具。它的名字来源于GlobalRegularExpressionPrint(全局正则表达式打印),它的主要功能是根据指定的模式(正则表达式)在文本文件中搜索并打印匹配的行。grep非常强大且灵活,可以用于日志分析、文件过滤、代码搜索等多种场......
  • 软件测试|Linux三剑客之sed命令详解
    简介sed(StreamEditor)是一款流式文本编辑器,在Linux和类Unix系统中广泛使用。它的设计目的是用于对文本进行处理和转换,可以用于替换、删除、插入、打印等操作。sed命令通过逐行处理文本,允许您使用简单的命令来编辑大量文本数据。本文将详细介绍sed命令的基本用法和一些常见的......
  • shell 学习之文本处理工具
    视频:07_小工具使用diff【正常模式】_哔哩哔哩_bilibili1.grep 2.cut 3.sort  4.uniq 5.tee从标准输入读取并写到标准输出和文件,即双向覆盖重定向(屏幕输出/文本输入)。 6.diff 逐行比较文件的不同。 语法:diff[选项]file1file2 1)正常显示......
  • 使用grep做文本的过滤
    常与管道符(|)结合在一起使用管道符piping:用于前一个命令的输出当作后一个命令的输入。常用于连接多个命令┌──(root㉿kali)-[~/work/exam]└─#psaux|grepapache2root419460.00.265682304pts/1S+17:260:00grep--color=autoapache2......
  • Linux文本三剑客sed
    目录脚本格式sed即StreamEDitorsed是编辑器sed格式sed[选项]...{sed自己的脚本}{输入文件}...sed'脚本语言'sed自己的脚本语言脚本'地址'+'命令'脚本'命令'#没有地址就是全文选项:-n不输出模式空间内容到屏幕,即不自动打印-r使用扩展正则表达式......
  • linux的grep功能介绍与使用
    环境centos7.9众所周知,在Linux操作系统中,"grep"命令是一种强大而常用的文本搜索工具。它用于在文件中查找特定的模式,并显示包含该模式的行。"grep"命令不仅可以搜索文件内容,还可以结合各种选项和正则表达式,提供丰富的搜索功能。本文档将介绍"grep"命令的基本用法和一些常用的选......
  • 华为认证欧拉openEuler-HCIA文本编辑器及文本处理
    文本编辑器及文本处理文本编辑器介绍常见的Linux文本编辑器有:emacsnanogeditkeditvivimLinux文本编辑器-emacsemacs是一款功能强大的编辑器,与其说是一款编辑器,它更像一个操作系统。emacs带有内置的网络浏览器、IRC客户端、计算器,甚至是俄罗斯方块。当然,emacs需要在图形......
  • Linux文本处理之sed
    上次我们介绍了awk的用法,今天我接着了解sed的功能和用法。Sed可按照脚本命令来处理和编辑文件,简化对文件的重复性操作。命令格式sed命令格式:sed[options]'command'file(s);sed脚本格式:sed[options]-fscriptfilefile(s);选项参数-e:直接在命令行模式上进行sed动......
  • idea实用插件推荐(1)-Grep Console
    1.简介GrepConsole可以根据日志等级设置不同的颜色,效果如下:安装点击File->Settings在搜索框里输入GrepConsole,点击install即可安装使用安装成功后,在idea控制台右键点击ShowGrepConsoleStatisticsinConsole,即可设置对应日志级别的颜色公众号:1号程序员,关注回复B0......