草庐IT

grep命令基础知识

南通市民李某人 2023-04-14 原文
Grep
全称Global Regular Expression Print,表示全局正则表达式
是一个强大的文本搜索工具,采用正则匹配
1、命令格式
grep [options] files


2、主要参数
-c: 只输出匹配行的数目
-i: 不区分大小写
-n:显示匹配航以及行号
-l:查询多文件的时候只输出包含匹配字符的文件名
-v:反向匹配,即显示不匹配的行
-h: 查询的时候不适用文件名
-s:不显示错误信息


3、部分正则表达式
\       反义字符:如"\"\""表示匹配""
^$   开始和结束
[]   单个字符,[A]
[ - ] 匹配一个范围,[0-9a-zA-Z]匹配所有数字和字母
*   前面的字符出现0次或者多次
+   前面的字符出现了一次或者多次
.   任意字符

4、经典场景

除非要精确区分大小写,否则请加上-i来忽略大小写

(1)结合find命令和管道
  你的一个音乐文件夹里有多种格式的文件,而你只想找到艺术家jay的mp3文件,并且不含有任何的混合音轨

 
  1. [root@localhost ~] # grep -lr baidu .
  2. ./file.txt
  3. ./test/test.txt

查找控制台输出语句的位置文件

-l Only the names of files containing selected lines are written to standard output.
             grep will only search a file until a match has been found, making searches potentially
             less expensive.  Pathnames are listed once per file searched.  If the standard input
             is searched, the string ``(standard input)'' is written.

info AppiumDoctor  ✔ Xcode is installed at: /Applications/Xcode.app/Contents/Developer
[Error: Could not detect Mac OS X Version from sw_vers output: '10.12.6']
CrettdeMacBook-Air:crett$ grep -rl "Could not detect Mac OS X Version from sw_vers output:" /Applications/Appium.app/

/Applications/Appium.app//Contents/Resources/node_modules/appium/node_modules/appium-support/build/lib/system.js
/Applications/Appium.app//Contents/Resources/node_modules/appium/node_modules/appium-support/lib/system.js
/Applications/Appium.app//Contents/Resources/node_modules/appium-support/build/lib/system.js
/Applications/Appium.app//Contents/Resources/node_modules/appium-support/lib/system.js
CrettdeMacBook-Air:


(5)--line-buffered 打开buffering 模式
  你有一个文件是动态的,它不断地添加信息到文件的尾部,而你想要输出包含某些信息的行。即持续的grep一个动态的流


[root@localhost ~]#tail -f file | grep --line-buffered your_pattern 


(6)结合ps查找进程
[root@localhost ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init

这里我们看到了grep init我们执行的命令也被列出来了
如果不想要这一行,我们可以这么改命令
[root@localhost ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients

(7)查找不包含某一个目录
[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

例子

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com
这时候如果我们不想包含test目录

[root@localhost ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
如果报错

grep: unrecognized option `--exclude-dir=test'

说明版本过老,更新下就ok

(8)查找IP地址
这里用到了-o和-P命令
我们通过man grep查看
-o, --only-matching:
              Show only the part of a matching line that matches PATTERN.
-P, --perl-regexp:
              Interpret PATTERN as a Perl regular expression.
也就是说-o,只显示匹配行中匹配正则表达式的那部分
-P,作为Perl正则匹配
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123

(9)查找邮箱

[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt

例子

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
njuhwc@163.com
njuhwc@gmil.com

有关grep命令基础知识的更多相关文章

  1. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  2. postman接口测试工具-基础使用教程 - 2

    1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,

  3. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  4. 软件测试基础 - 2

    Ⅰ软件测试基础一、软件测试基础理论1、软件测试的必要性所有的产品或者服务上线都需要测试2、测试的发展过程3、什么是软件测试找bug,发现缺陷4、测试的定义使用人工或自动的手段来运行或者测试某个系统的过程。目的在于检测它是否满足规定的需求。弄清预期结果和实际结果的差别。5、测试的目的以最小的人力、物力和时间找出软件中潜在的错误和缺陷6、测试的原则28原则:20%的主要功能要重点测(eg:支付宝的支付功能,其他功能都是次要的)80%的错误存在于20%的代码中7、测试标准8、测试的基本要求功能测试性能测试安全性测试兼容性测试易用性测试外观界面测试可靠性测试二、质量模型衡量一个优秀软件的维度①功能性功

  5. ES基础入门 - 2

    ES一、简介1、ElasticStackES技术栈:ElasticSearch:存数据+搜索;QL;Kibana:Web可视化平台,分析。LogStash:日志收集,Log4j:产生日志;log.info(xxx)。。。。使用场景:metrics:指标监控…2、基本概念Index(索引)动词:保存(插入)名词:类似MySQL数据库,给数据Type(类型)已废弃,以前类似MySQL的表现在用索引对数据分类Document(文档)真正要保存的一个JSON数据{name:"tcx"}二、入门实战{"name":"DESKTOP-1TSVGKG","cluster_name":"elasticsear

  6. ruby - 从 Ruby : capturing the output while displaying the output? 运行 shell 命令 - 2

    我有一个问题。我想从另一个ruby​​脚本运行一个ruby​​脚本并捕获它的输出信息,同时让它也输出到屏幕。亚军#!/usr/bin/envrubyprint"Enteryourpassword:"password=gets.chompputs"Hereisyourpassword:#{password}"我运行的脚本文件:开始.rboutput=`runner`putsoutput.match(/Hereisyour(password:.*)/).captures[0].to_s正如您在此处看到的那样,存在问题。在start.rb的第一行,屏幕是空的。我在运行程序中看不到“输入您的密

  7. ruby - 是否有将图像文件转换为 ASCII 艺术的命令行程序或库? - 2

    有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/

  8. ruby - 在 Ruby 的 if 语句中检查 bash 命令 - 2

    如何在Ruby的if语句中检查bash命令的返回值(true/false)。我想要这样的东西,if("/usr/bin/fswscell>/dev/null2>&1")has_afs="true"elsehas_afs="false"end它会提示以下错误含义,它总是返回true。(irb):5:warning:stringliteralincondition正确的语法是什么?更新:/usr/bin/fswscell寻找afs安装和运行状态。它会抛出这样的字符串,Thisworkstationbelongstocell如果afs没有运行,命令以状态1退出 最

  9. ruby - 可以正常中断的来自 Rake 的长时间运行的 shell 命令? - 2

    在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa

  10. ruby - Capistrano 中的执行、测试和捕获命令有什么区别? - 2

    关于SSHkit-Github它说:Allbackendssupporttheexecute(*args),test(*args)&capture(*args)来自SSHkit-Rubydoc,我明白execute实际上是test的别名?test之间有什么区别?,execute,capture在Capistrano/SSHKit中我应该什么时候使用? 最佳答案 执行只是执行命令。使用非0退出引发错误。测试方法的行为与execute完全相同,但是它返回bool值(true如果命令以0退出,而false否则)。它通常用于控制任务中的流程

随机推荐