[svc]linux正则及grep常用手法
正则测试
可以用sublime等工具快速的检测正则是否合适


china : 匹配此行中任意位置有china字符的行
^china : 匹配此以china开关的行
china$ : 匹配以china结尾的行
^china$ : 匹配仅有china五个字符的行
[Cc]hina : 匹配含有China或china的行
Ch.na : 匹配包含Ch两字母并且其后紧跟一个任意字符之后又有na两个字符的行
Ch.*na : 匹配一行中含Ch字符,并且其后跟0个或者多个字符,再继续跟na两字符
扩展正则
**口诀: 温家星**
? : 匹配前面正则表达式的零个或一个扩展
+ : 匹配前面正则表达式的一个或多个扩展
* : 前面出现0个或多个
{m,n} : 前面出现m,n
| : 匹配|符号前或后的正则表达式
() : 匹配方括号括起来的正则表达式群,如g(la|oo)d
[a-z] : 序列,如[a-Z] [0-9] [Cc]
几个例子: 1.+代表出现1次或多次
[a-z]+
[a-c]d+
d[a-c]+
(ab)+
(d[a-c]+)+
2.()的例子
[root@n1 test]# egrep 'g(la|oo)d' test.md
good
glad
来自grep的man手册
Repetition
A regular expression may be followed by one of several repetition operators:
? The preceding item is optional and matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times. This is a GNU extension.
{n,m} The preceding item is matched at least n times, but not more than m times.
grep参数
-i, --ignore-case 不区分大小写
-v, --invert-match 排除
-c, --count 统计url出现次数
-E --extended-regexp 等价于egrep, grep -E 'a|b'
-n, --line-number
-r, --recursive 按照目录
-o, --only-matching 只显示匹配行中匹配正则表达式的那部分
-C -A -B
grep -nr
grep -oP
过滤ip

192.168.100.100
ifconfig|grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}"
过滤邮箱

cat >>tmp.txt<<EOF
iher-_@qq.com
hello
EOF
cat tmp.txt|grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+"
统计baidu关键字的url在这个大文件中出现的次数
$ cat >file.txt<<EOF
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
EOF
grep -cn ".*baidu.com.*" file.txt
3
过滤jay的,非混音轨的mp3的内容
[root@lanny ~]# cat jay.txt
Jay_01_remix.mp3
jAy_02_rEmix.mp3
jaY_03_Remix.mp3
jay_05_ori.mp3
lanny.mp4
mo.3gp
lanny.mp3
思路: 先过滤mp3的内容,然后再mp3中找出jay的,然后再jay中找出非remix的内容
[root@lanny ~]# grep -E ".mp3" jay.txt|grep -i "jay"|grep -vi "remix"
jay_05_ori.mp3
[svc]linux正则及grep常用手法的更多相关文章
- [svc]linux正则实战(grep/sed/awk)
企业实战: 过滤ip 过滤出第二行的 192.168.2.11. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ine ...
- Linux正则和grep命令
设置命令的默认参数和别名 每次都要输入 ls -l ,烦不烦,我想用 ll 来表示 ls -l, 可以,只要在 ~/.bashrc 中加上 alias ll='ls -l' ,然后运行 source ...
- Linux三剑客之grep常用参数详细总结
三剑客grep总结 grep : Linux三剑客老三 过滤需要的内容 参数: grep一般常用参数: -a :在二进制文件中,以文本文件的方式搜索数据 -c :计算找到 ’ 搜索字符串 ...
- [svc]linux常用手头命令-md版-2017年11月12日 12:31:56
相关代码 curl命令-网站如果3次不是200或301则报警 curl -o /dev/null -s -w "%{http_code}" baidu.com -k/--insec ...
- Linux中Grep常用的15个例子【转】
转自:https://www.aliyun.com/jiaocheng/1390860.html?spm=5176.100033.1.9.6a1e41e8Pdjynm 摘要:Grep命令主要用于从文件 ...
- Linux 使用grep过滤多个条件及grep常用过滤命令
这篇文章主要介绍了Linux 使用grep筛选多个条件及grep常用过滤命令,需要的朋友可以参考下 cat log.txt | grep 条件: cat log.txt | grep 条件一 | gr ...
- [转帖]「日常小记」linux中强大且常用命令:find、grep
「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...
- linux命令系列 grep
grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ...
- centos 正则,grep,egrep,流式编辑器 sed,awk -F 多个分隔符 通配符 特殊符号. * + ? 总结 问加星 cat -n nl 输出文件内容并加上行号 alias放~/.bash_profile 2015-4-10 第十三节课
centos 正则,grep,egrep,流式编辑器 sed,awk -F 多个分隔符 通配符 特殊符号. * + ? 总结 问加星 cat -n nl 输出文件内容并加上行号 alias放~ ...
随机推荐
- yii框架的部署方法
yii框架(yii framework)的部署方法 刚開始学习的人来说,部署yii框架还是有一定难度的,Yii是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web ...
- django之创建第6个项目-过滤器
1.views.PY # Create your views here. #coding:utf-8 from django.http import HttpResponse import datet ...
- java 自增和自减运算符
/** 自增和自减运算符: ++: 如果是++b,则表示先对变量b+1,再执行其他的操作: 如果是b++,则表示先执行表达式操作,再对变量自身+1 --: 用法和++相同 */ //Test.java ...
- Centos7 安装 docker-ce
本文参考官网地址:https://docs.docker.com/install/linux/docker-ce/centos/#os-requirements 1.卸载旧版本的docker $ su ...
- Arduino——My-Clock项目 发布时间:2018-12-31
技术:Arduino.光敏传感器.DHT11.DS1302.OLED显示屏 概述 项目My-Clock是一个环境监测时钟,接入光敏传感器和温湿度传感器监测环境信息,加入DS1302模块用于获取时间 ...
- Axure快速原型教程02--创建页面和设置界面
目录 Axure快速原型教程02--创建页面和设置界面 Axure快速原型教程01--原型说明下载和安装 首先,在左侧的面板中,我们发现有一个叫sitemap的面板,这个面板就是我们的一个个的页面了, ...
- yum安装nodejs 8
Install Node.js 8 on Enterprise Linux Distributions To install Node.js 8 on Enterprise Linux distros ...
- maven 下载 源码和javadoc命令(转)
转自:http://blog.csdn.net/topwqp/article/details/8902863 maven 下载 源码和javadoc命令 目录(?)[+] Maven命令下载源码和ja ...
- 实测iOS Dynamic Framework 对 App 启动时间的影响效果
最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...
- Magento模型与ORM基础
参考网址:http://www.ruiwant.com/magento-for-dev-part-5-magento-models-and-orm-basics.html