lsof |grep delete卡住_Linux:命令file 和 grep

这篇文章将从三个方面聊一聊Linux中的file和grep命令

  • 介绍grep命令(egrep,fgrep),示例
  • 介绍find命令,示例
  • grep与find命令使用对比,示例
  1. 介绍grep命令
  • grep的前言

grep据说是linux数据处理三剑客之一(另外两个分别是sed,awk)也可以感受到grep命令在Linux的数据处理方面的强大之处。

grep is a command line utility in Unix and Linux systems. It is used for finding a search patterns in the content of a given file.
With its unusual name, you may have guessed that grep is an acronym. This is at least partially true, but it depends on who you ask.
According to reputable sources, the name is actually derived from a command in a UNIX text editor called ed. In which, the input g/re/p performed a global (g) search for a regular expression (re), and subsequently printed (p) any matching lines.
The grep command does what the g/re/p commands did in the editor. It performs a global research for a regular expression and prints it. It is much faster at searching large files.
This is the official narrative, but you may also see it described as Global Regular Expression ( Processor | Parser | Printer). Truthfully, it does all of that.

翻译过来就是:

grep是Unix和Linux系统中的一个命令行实用程序。它用于在给定文件的内容中查找搜索模式。有了它不同寻常的名字,你可能已经猜到grep是一个缩略词。这至少在一定程度上是正确的,但这取决于你问谁。根据可靠的消息来源,这个名字实际上是从UNIX文本编辑器ed中的一个命令派生出来的。在这个命令中,输入g/re/p执行正则表达式(re)的全局(g)搜索,然后打印(p)任何匹配的行。grep命令执行g/re/p命令在编辑器中所做的工作。它对正则表达式执行全局研究并打印它。它在搜索大文件时要快得多。[1]

lsof |grep delete卡住_Linux:命令file 和 grep_第1张图片
grep的官方解释 图源:linuxhandbook

这是官方的叙述,但是您也可以看到它被描述为全局正则表达式(Processor | Parser | Printer)。说实话,这一切都是这样。[1]

上图的意思是:grep命令中g代表global search意思是全局搜索,re代表regular expression 意思就是正则表达式, p 代表的是print 输出。合起来就是grep命令可以通过正则表达式进行全局搜索然后输出结果。

  • grep命令及参数

grep命令的作用:过滤

语法:grep 关键字 文件

先举个例子在聊一聊不得不说的正则表达式(regex)语法,这里用的例子还是linuxhandbook里面的例子

lsof |grep delete卡住_Linux:命令file 和 grep_第2张图片

可以通过这个例子发现一些grep的规律

1 grep是模糊匹配(这一点在后面会提到和find不一样)

2 grep命令搜索出来的默认标红的是所有匹配到的字符

3 “grep 5 phone.txt"表示在phone.txt中搜索5

...

至于相关的参数在--help中就有详细的说明,这里也列一下吧

grep --help
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c

正则表达式选择与解释:
  -E, --extended-regexp     PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
  -F, --fixed-strings       PATTERN 是一组由断行符分隔的定长字符串。
  -G, --basic-regexp        PATTERN 是一个基本正则表达式(缩写为 BRE)
  -P, --perl-regexp         PATTERN 是一个 Perl 正则表达式
  -e, --regexp=PATTERN      用 PATTERN 来进行匹配操作
  -f, --file=FILE           从 FILE 中取得 PATTERN
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         强制 PATTERN 仅完全匹配字词
  -x, --line-regexp         强制 PATTERN 仅完全匹配一行
  -z, --null-data           一个 0 字节的数据行,但不是空行

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

输出控制:
  -m, --max-count=NUM       NUM 次匹配后停止
  -b, --byte-offset         输出的同时打印字节偏移
  -n, --line-number         输出的同时打印行号
      --line-buffered       每行输出清空
  -H, --with-filename       为每一匹配项打印文件名
  -h, --no-filename         输出时不显示文件名前缀
      --label=LABEL         将LABEL 作为标准输入文件名前缀
  -o, --only-matching       show only the part of a line matching PATTERN
  -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=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

文件控制:
  -B, --before-context=NUM  打印以文本起始的NUM 行
  -A, --after-context=NUM   打印以文本结尾的NUM 行
  -C, --context=NUM         打印输出文本NUM 行
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --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)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。
如果少于两个FILE 参数,就要默认使用-h 参数。
如果有任意行被匹配,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。

请将错误报告给: [email protected]
GNU Grep 主页: 
GNU 软件的通用帮助: 

在这里,注意到这几行

-i, --ignore-case 忽略大小写

-x, --line-regexp 强制

-v, --invert-match select non-matching lines #The v option reverses the sense of the match and prints all lines that do not contain apple, as in this example.[2] #-v相当于反向匹配,取补集

lsof |grep delete卡住_Linux:命令file 和 grep_第3张图片
  • grep和egrep,fgrep的区别

这里e代表extended,f代表fixed。egrep可以使用extended(拓展的)正则表达式(区别于基础正则表达式),fgrep则完全无法使用正则表达式。egrep相当于 ‘ grep -E ’,fgrep相当于 ‘ grep -F ’[3]

参考

  1. ^abChristopher Murray:what is grep https://linuxhandbook.com/what-is-grep/
  2. ^超星百科词条:grep http://ency.chaoxing.com/mdetail/86D2FECA88702C832E76A5CE5C2A9D57?appId=1000
  3. ^Christopher Murray:Explained! The Difference Between grep, egrep, and fgrep Commands https://linuxhandbook.com/grep-egrep-fgrep/

你可能感兴趣的:(lsof,grep,delete卡住)