2022-10-07小白笔记0

grep [关键字][文件名]查找文件中关键字,可查多个文件,支持正则表达式
grep -v 排除搜索出结果中的关键字

bash
momen@momen-PC:~/Documents$ ps -ef|grep bash|grep -v QQ
momen      48078       1  0 10:42 ?        00:00:00 /usr/bin/bash /opt/durapps/fantascene-dynamic-wallpaper/startdesktop.sh
momen      48385   48377  0 10:42 pts/2    00:00:00 /bin/bash
momen      48609   48377  0 10:43 pts/3    00:00:00 /bin/bash
root       48733   48732  0 10:43 pts/3    00:00:00 bash
momen      48782   48781  0 10:43 pts/3    00:00:00 bash
root       48808   48807  0 10:43 pts/3    00:00:00 bash
momen      48820   48819  0 10:44 pts/3    00:00:00 -bash
root       50103   50102  0 10:49 pts/2    00:00:00 bash
momen      50117   48377  0 10:49 pts/0    00:00:00 /bin/bash
root       51246   51245  0 10:53 pts/0    00:00:00 bash
momen      51265   51264  0 10:53 pts/0    00:00:00 -bash
root       51308   51307  0 10:53 pts/0    00:00:00 bash
momen      51338   51337  0 10:53 pts/0    00:00:00 bash
momen      53154    6142  0 11:01 ?        00:00:00 bash /home/momen/.local/share/Steam/steam.sh steam://rungameid/730
momen      55758   48377  0 11:09 pts/1    00:00:00 /bin/bash
momen      57501   55758  0 11:15 pts/1    00:00:00 grep bash

momen@momen-PC:~/Documents$ ps -ef|grep bash
momen      39238       1  0 10:25 ?        00:00:00 /bin/bash /usr/bin/deepin-wine6-stable c:/Program Files/Tencent/QQ/Bin/QQ.exe
momen      48078       1  0 10:42 ?        00:00:00 /usr/bin/bash /opt/durapps/fantascene-dynamic-wallpaper/startdesktop.sh
momen      48385   48377  0 10:42 pts/2    00:00:00 /bin/bash
momen      48609   48377  0 10:43 pts/3    00:00:00 /bin/bash
root       48733   48732  0 10:43 pts/3    00:00:00 bash
momen      48782   48781  0 10:43 pts/3    00:00:00 bash
root       48808   48807  0 10:43 pts/3    00:00:00 bash
momen      48820   48819  0 10:44 pts/3    00:00:00 -bash
root       50103   50102  0 10:49 pts/2    00:00:00 bash
momen      50117   48377  0 10:49 pts/0    00:00:00 /bin/bash
root       51246   51245  0 10:53 pts/0    00:00:00 bash
momen      51265   51264  0 10:53 pts/0    00:00:00 -bash
root       51308   51307  0 10:53 pts/0    00:00:00 bash
momen      51338   51337  0 10:53 pts/0    00:00:00 bash
momen      55758   48377  0 11:09 pts/1    00:00:00 /bin/bash
momen      57501   55758  0 11:15 pts/1    00:00:00 grep bash

grep -i 不匹配大小写

bash
momen@momen-PC:~/Documents$ grep apple 1.txt
apple
momen@momen-PC:~/Documents$ grep -i apple 1.txt
apple
Apple

-E 正则表达式

参数

  • -a 或 --text : 不要忽略二进制的数据。
  • -A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
  • -b 或 --byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
  • -B<显示行数> 或 --before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
  • -c 或 --count : 计算符合样式的列数。
  • -C<显示行数> 或 --context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
  • -d <动作> 或 --directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
  • -e<范本样式> 或 --regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
  • -E 或 --extended-regexp : 将样式为延伸的正则表达式来使用。
  • -f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
  • -F 或 --fixed-regexp : 将样式视为固定字符串的列表。
  • -G 或 --basic-regexp : 将样式视为普通的表示法来使用。
  • -h 或 --no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
  • -H 或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
  • -i 或 --ignore-case : 忽略字符大小写的差别。
  • -l 或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。
  • -L 或 --files-without-match : 列出文件内容不符合指定的样式的文件名称。
  • -n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
  • -o 或 --only-matching : 只显示匹配PATTERN 部分。
  • -q 或 --quiet或--silent : 不显示任何信息。
  • -r 或 --recursive : 此参数的效果和指定"-d recurse"参数相同。
  • -s 或 --no-messages : 不显示错误信息。
  • -v 或 --invert-match : 显示不包含匹配文本的所有行。
  • -V 或 --version : 显示版本信息。
  • -w 或 --word-regexp : 只显示全字符合的列。
  • -x --line-regexp : 只显示全列符合的列。
  • -y : 此参数的效果和指定"-i"参数相同。
bash
文本文件中查找字符串
grep str /tmp/test #在文件test中寻找str
grep ^str /tmp/test #在文件中查找以str开始的行
grep str -r /tmp/* #在目录/tmp及其目录中查找str

本文作者:星火文档中心

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!