首页 > 系统相关 >shell实战正则三贱客—grep

shell实战正则三贱客—grep

时间:2023-12-15 14:23:08浏览次数:19  
标签:00 shell grep hllke root hello localhost 三贱客

三剑客
三剑客特点,及应用场景
命令 特点 场景
grep 过滤 grep过滤速度是最快的
sed 替换,修改文件内容,取行 如果要进行替换/修改文件内容,取出某个范围的内容(从早上10:00到11:00)
awk 取列,统计结算 取列
对比,比较
统计,计算(awk数组)
shell实战正则三贱客—grep
选项 含义
-E ==egrep 支持扩展正则
-A -A5匹配你要的内容显示接下来的5行 (after)
-B -A5匹配你要的内容显示接上面的5行 (before)
-C 匹配你要的内容,并且显示上下5行(context)
-c 统计出现了多少行,类似wc -l
-v 排除,或者取反,按行为单位 |grep -v grep排除自个
-n 显示行号
-i 忽略大小写
-w 精确匹配 ; =='\b \b' =='\< \>' 也可以用这两类方式达到类似效果。
[root@localhost ~]# seq 1 10 |grep -B3 3
1
2
3
[root@localhost ~]# seq 1 10 |grep -C2 3
1
2
3
4
5
[root@localhost ~]# seq 1 10 |grep -C 2 3
1
2
3##上下2行
4
5
[root@localhost ~]# seq 1 10 |grep -c 2 3
grep: 3: 没有那个文件或目录
[root@localhost ~]# seq 1 10 |grep  -c  3
1
[root@localhost ~]# seq 1 10 |grep    3
3
[root@localhost ~]# seq 1 10 |grep    3|wc -l
1
[root@localhost ~]# 
###grep -v 一般用于排除自个,当然还有写特殊写发用其他方式
[root@localhost ~]# ps  -ef|grep crond
root      1221     1  0 11:09 ?        00:00:00 /usr/sbin/crond -n
root      6301  2724  0 14:00 pts/0    00:00:00 grep --color=auto crond
[root@localhost ~]# ps  -ef|grep crond|grep -v grep
root      1221     1  0 11:09 ?        00:00:00 /usr/sbin/crond -n
[root@localhost ~]# ps  -ef|grep [c]rond   #####特殊的其他方式写法也能实现。
root      1221     1  0 11:09 ?        00:00:00 /usr/sbin/crond -n
[root@localhost ~]# 
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep -w hello
helloworld hllke hello heLlow
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep -i hello
helloworld hllke hello heLlow
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep  'hello'
helloworld hllke hello heLlow
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep  '\bhello\b'  ####\b表示边界 ==grep -w
helloworld hllke hello heLlow
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep  '\<hello\>'    ####\<和\>表示边界 ==grep -w
helloworld hllke hello heLlow
[root@localhost ~]# 
helloworld hllke hello heLlow
[root@localhost ~]# echo helloworld  hllke hello heLlow |grep -w hel  
[root@localhost ~]#

标签:00,shell,grep,hllke,root,hello,localhost,三贱客
From: https://www.cnblogs.com/xjianbing/p/17903280.html

相关文章

  • shell实战正则三贱客——sed
    shell实战正则三贱客——sed特点及格式sedstreameditor:流编辑器,sed把处理的内容(文件),当做是水,源源不断的进行处理,一行一行读取文件,直到文件末尾。sed格式命令选项sed命令功能说明参数sed-r's#oldboy#oldgirl#g'单引号里面的s表示替换功能;g表示一......
  • shell实战正则表达式
    正则表达式匹配有规律的东西:手机号,身份证号,匹配日志什么正则表达式,regularexpression(RE)正则:使用一些符号表达重复出现,大小写,开头/结尾含义。应用场景哪些可以正则表达式Linux三剑客使用,一些开发语言应用场景过滤有规律的内容,尤其是日志正则符号......
  • Mysql慢日志getshell
    Mysql慢日志getshellshowvariableslike'%slow%';Variable_nameValuelog_slow_queriesOFFslow_launch_time2slow_query_logOFFslow_query_log_fileC:\phpStudy\PHPTutorial\MySQL\data\WIN-374NAWYudt-slow.logsetGLOBALsl......
  • shell 脚本中的 '-f' 和 '-d' 分别代表什么意思
    shell脚本中,'-f'和'-d'是用于测试文件类型的条件表达式。1、'-f'表达式:表达式:'[-ffile]'描述:判断给定路径是否是一个常规文件(regularfile)。常规文件是指不是目录或设备文件的文件。示例:if[-f/path/to/file];thenecho"这是一个文件。"fi2、'-d'表达式:......
  • shell补-shell数组
    shell补-shell数组回顾变量的赋值方法直接赋值:a=1引用命令结果:ip=$(hostname-I|awk'{print$1}')通过read交互示参数传递:脚本/函数参数传参不了解数组之前可以用whilereadline这类方法语法:数组名称[下标],从0开始####赋值比较繁琐[root@localho......
  • shell补-命令补缺-命令回顾(重要)
    shell补-命令补缺-命令回顾命令行概述参数(parameter)选项(option)选项:命令的不同功能参数:把是什么东西传递给命令(目录/文件....)命令选项(optiongs)参数parameterls-l/etc/hostssh-x/etc/init.d/networkrestart在linux命令行下查看命令......
  • shell补-特殊玩法-cut命令tar以及past和join
    shell补-特殊玩法-cut命令是awk的阉割版具体情况,后面详看shell补-特殊玩法-tar压缩指定目录案例案例:指定目录路径,脚本自动将该使用tar命令打包备份到/data目录#!/bin/bashread-p"Inputdirtobackup:"dirif[-d"${dir}"];then #tar-zcvf/data/$(date+%F_%H)......
  • shell补-特殊玩法-shell编程debug
    shell补-特殊玩法-shell编程debugdebug思想debug测试单步执行脚本自个调试,用注释,或者echo自个打印输出啥的,就这么搞bash-x整个脚本调试set与开关debug(适用于脚本或者命令行都可以)set-x开始debugset+x结束debug##在脚本启用set;set-x开始,set+x结尾......
  • shell补-循环案例-循环case
    shell补-循环案例-循环casecase结构条件的语法格式;一般应用菜单的功能case$变量名in"值1")###如果变量的值1,则执行此程序1;;"值2")###如果变量的值2,则执行此程序2;;###....省略其他分支YES|yes|Yes)#####此处的值,可以带些符号,比如|表示或者;也可以用[a-z]感觉......
  • shell补-特殊玩法-生成随机字符串
    shell补-特殊玩法-生成随机字符串方法1:md5sum方法2:tr+/dev/urandom方法3:内置变量RANDOM;#方法1[root@localhostser]#opensslrand-base64108/54arQpCmQ12Q==[root@localhostser]##方法2必备[root@localhostser]#date+%N|md5sum###给日期加密;可以写其......