优秀的编程知识分享平台

网站首页 > 技术文章 正文

Linux三剑客-grep(linux三剑客是什么)

nanyue 2024-09-10 16:12:08 技术文章 7 ℃
  • grep命令的常用格式为:grep [选项] ”模式“ [文件]
moke@moke:/mnt/c/Users/moke_$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions 开启扩展(Extend)的正则表达式
  -F, --fixed-strings       PATTERNS are strings 模式字符串
  -G, --basic-regexp        PATTERNS are basic regular expressions 普通的表示法来使用
  -P, --perl-regexp         PATTERNS are Perl regular expressions 用perl正则表达式
  -e, --regexp=PATTERNS     use PATTERNS for matching 指定字符串做为查找文件内容的样式
  -f, --file=FILE           take PATTERNS from FILE  让grep查找符合规则条件的文件内容
  -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 此参数的效果和指定"-d 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)
  • 实例
# 输出文件中特定字符的行
moke@moke:/mnt/c/Users/moke_$ grep -F start Desktop/mysql-start.bat
net start mysql
# 输出文件中特定字符的行
moke@moke:/mnt/c/Users/moke_$ grep -e 'mysql' Desktop/mysql-start.bat
net start mysql
# 输出文件2中于文件1中相同的内容
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -f mysql-start.bat mysql-stop.bat

echo mysql
# 忽略大写小写
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -i 'MYSQL' mysql-start.bat
net start mysql
echo mysql

# 列出符合字符的列
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -w 'mysql' mysql-start.bat
net start mysql
echo mysql

# 全行匹配
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -x 'echo mysql' mysql-start.bat
echo mysql

# -z文字中间空行展示
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -z 'mysql' mysql-start.bat
net start mysql

echo mysqlmoke@moke:/mnt/c/Users/moke_/Desktop$

# -s:错误信息过滤
echo mysqlmoke@moke:/mnt/c/Users/moke_/Desktop$ grep -z 'mysql' mysql-start.bat
net start mysql
error: 1212
echo mysqlmoke@moke:/mnt/c/Users/moke_/Desktop$ grep -s 'mysql' mysql-start.bat
net start mysql
echo mysql

# -v 显示不匹配的所有行
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -v 'mysql' mysql-start.bat
error: 1212

# -V 显示版本信息
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -V
grep (GNU grep) 3.4
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

# -m 匹配几个
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -m 1 'mysql' mysql-start.bat
net start mysql
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -m 2 'mysql' mysql-start.bat
net start mysql
echo mysql

# -n 显示所匹配行数
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -n 'mysql' mysql-start.bat
1:net start mysql
3:echo mysql

# -H 显示匹配文件名
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -H 'mysql' mysql-start.bat
mysql-start.bat:net start mysql
mysql-start.bat:echo mysql

# -h 不显示匹配文件名
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -h 'mysql' mysql-start.bat
net start mysql
echo mysql

# -o 只显示匹配信息
moke@moke:/mnt/c/Users/moke_/Desktop$ grep -o 'mysql' mysql-start.bat
mysql
mysql

# -d read/recurse/skip 查找目录下的匹配
moke@moke:/mnt/c/Users/moke_$ grep -d read 'mysql' Desktop/*
Desktop/mysql-start.bat:net start mysql
Desktop/mysql-start.bat:echo mysql
Desktop/mysql-stop.bat:net stop mysql
Desktop/mysql-stop.bat:echo mysql

# -r 效果和指定"-d recurse"参数相同
moke@moke:/mnt/c/Users/moke_$ grep -r 'mysql' Desktop/*
Desktop/mysql-start.bat:net start mysql
Desktop/mysql-start.bat:echo mysql
Desktop/mysql-stop.bat:net stop mysql
Desktop/mysql-stop.bat:echo mysql

# -L 显示不匹配文件名
moke@moke:/mnt/c/Users/moke_$ grep -L 'mysql' Desktop/*
Desktop/IMG_0002087+7电子.jpg
Desktop/Internet Download Manager.lnk
Desktop/QQ浏览器.lnk
Desktop/desktop.ini
Desktop/微信图片_20210609104726.jpg
Desktop/本课程学习必读.pdf
Desktop/迅雷.lnk
# -l 显示匹配文件名
moke@moke:/mnt/c/Users/moke_$ grep -l 'mysql' Desktop/*
Desktop/mysql-start.bat
Desktop/mysql-stop.bat

# -c 显示匹配行数
moke@moke:/mnt/c/Users/moke_$ grep -c 'mysql' Desktop/mysql-start.bat
2

# -T 使用tab 对齐
moke@moke:/mnt/c/Users/moke_$ grep -T 'mysql' Desktop/*
Desktop/mysql-start.bat:        net start mysql
Desktop/mysql-start.bat:        echo mysql
Desktop/mysql-stop.bat: net stop mysql
Desktop/mysql-stop.bat: echo mysql

# -Z 只展示匹配行内容
moke@moke:/mnt/c/Users/moke_$ grep -Z 'mysql' Desktop/mysql-start.bat
net start mysql
echo mysql

# -B 除了显示匹配的那一行之外,并显示该行之前的内容。
moke@moke:/mnt/c/Users/moke_$ grep -B  2 'mysql' Desktop/mysql-start.bat
1231
net start mysql
error: 1212

echo mysql
# -A 显示匹配行之后的内容
moke@moke:/mnt/c/Users/moke_$ grep -A  2 'mysql' Desktop/mysql-start.bat
net start mysql
error: 1212

echo mysql
0110110101111001011100110111000101101100

# -C 显示匹配之前之后的内容,数字代表显示几行
moke@moke:/mnt/c/Users/moke_$ grep -C  2 'mysql' Desktop/mysql-start.bat
1231
net start mysql
error: 1212

echo mysql
0110110101111001011100110111000101101100
1122

# -U 不剥离EOL结束符
moke@moke:/mnt/c/Users/moke_$ grep -U  'mysql' Desktop/*
Desktop/mysql-start.bat:net start mysql
Desktop/mysql-start.bat:echo mysql
Desktop/mysql-stop.bat:net stop mysql
Desktop/mysql-stop.bat:echo mysql

Tags:

最近发表
标签列表