grep

Grep help

grep还有很多开关, 例如-o就是我最喜欢的
/home/lee#grep --help
Usage: grep [OPTION]... PATTERN [FILE] ...
Search for PATTERN in each FILE or standard input.
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression
  -F, --fixed-strings       PATTERN is a set of newline-separated strings
  -G, --basic-regexp        PATTERN is a basic regular expression
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN as a regular expression
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to 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             print version information and exit
      --help                display this help and exit
      --mmap                use memory-mapped input if possible

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -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 the filename for each match
  -h, --no-filename         suppress the prefixing filename on output
      --label=LABEL         print LABEL as filename for standard input
  -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, -r, --recursive       equivalent to --directories=recurse
      --include=PATTERN     files that match PATTERN will be examined
      --exclude=PATTERN     files that match PATTERN will be skipped.
      --exclude-from=FILE   files that match PATTERN in FILE will be skipped.
  -L, --files-without-match only print FILE names containing no match
  -l, --files-with-matches  only print FILE names containing matches
  -c, --count               only print a count of matching lines per FILE
  -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 distinguish the matching string
                            WHEN may be `always', `never' or `auto'.
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.
With no FILE, or when FILE is -, read standard input.  If less than
two FILEs given, assume -h.  Exit status is 0 if match, 1 if no match,
and 2 if trouble.
### grep 命令简介 `grep` 是一个非常强大的文本搜索工具,它能够使用正则表达式来搜索文本,并将匹配的行打印出来。`grep` 支持多种模式匹配,包括基本正则表达式、扩展正则表达式以及固定字符串匹配等[^2]。 #### 基本用法 - **搜索特定模式**:最简单的使用方式是在指定文件中搜索某个模式。例如,要在一个文件中查找包含 "hello" 的所有行,可以使用如下命令: ```bash grep 'hello' filename.txt ``` - **忽略大小写**:如果希望搜索时不区分大小写,可以加上 `-i` 参数: ```bash grep -i 'hello' filename.txt ``` - **显示行号**:通过 `-n` 参数可以让 `grep` 显示匹配行的行号: ```bash grep -n 'hello' filename.txt ``` - **反向匹配**:想要找出不包含特定模式的行,可以利用 `-v` 选项: ```bash grep -v 'hello' filename.txt ``` #### 高级特性 - **使用正则表达式**:`grep` 支持使用正则表达式进行更复杂的模式匹配。比如,要查找以 "start" 开头并以 "end" 结尾的行,可以这样写: ```bash grep '^start.*end$' filename.txt ``` - **POSIX字符类**:为了确保在不同的字符编码环境中一致性,`grep` 支持 POSIX 定义的一些特殊字符类。例如,`[[:alnum:]]` 表示任何字母或数字字符: ```bash grep '[[:space:]]' ``` - **递归搜索**:当需要在整个目录结构中查找时,可以采用 `-r` 或者 `--recursive` 选项: ```bash grep -r 'pattern' /path/to/directory/ ``` - **组合多个模式**:可以通过 `-e` 参数指定多个搜索模式,或者直接在单个命令中用管道符 `|` 分隔不同的模式: ```bash grep -e 'pattern1' -e 'pattern2' filename.txt # 或者 egrep 'pattern1|pattern2' filename.txt ``` 以上仅是 `grep` 功能的一小部分展示。`grep` 还有许多其他选项和用法,可以根据具体需求探索更多高级功能。对于初学者来说,《鸟哥的Linux私房菜》提供了详尽的教程和实例,非常适合学习[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值