正则测试

可以用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参数

Grep命令详解-9个经典使用场景

    -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

附录:

sed深入浅出

awk深入浅出

[svc]linux正则及grep常用手法的更多相关文章

  1. [svc]linux正则实战(grep/sed/awk)

    企业实战: 过滤ip 过滤出第二行的 192.168.2.11. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ine ...

  2. Linux正则和grep命令

    设置命令的默认参数和别名 每次都要输入 ls -l ,烦不烦,我想用 ll 来表示 ls -l, 可以,只要在 ~/.bashrc 中加上 alias ll='ls -l' ,然后运行 source ...

  3. Linux三剑客之grep常用参数详细总结

    三剑客grep总结 grep  : Linux三剑客老三      过滤需要的内容 参数: grep一般常用参数: -a :在二进制文件中,以文本文件的方式搜索数据 -c :计算找到 ’ 搜索字符串 ...

  4. [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 ...

  5. Linux中Grep常用的15个例子【转】

    转自:https://www.aliyun.com/jiaocheng/1390860.html?spm=5176.100033.1.9.6a1e41e8Pdjynm 摘要:Grep命令主要用于从文件 ...

  6. Linux 使用grep过滤多个条件及grep常用过滤命令

    这篇文章主要介绍了Linux 使用grep筛选多个条件及grep常用过滤命令,需要的朋友可以参考下 cat log.txt | grep 条件: cat log.txt | grep 条件一 | gr ...

  7. [转帖]「日常小记」linux中强大且常用命令:find、grep

    「日常小记」linux中强大且常用命令:find.grep https://zhuanlan.zhihu.com/p/74379265 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍 ...

  8. linux命令系列 grep

    grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ...

  9. 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放~ ...

随机推荐

  1. 〖Linux〗在tmux同时使用bash和zsh

    个人有两份tmux配置文件: ~/.tmux.conf # 使用zsh,主要是日常使用,zsh太好使用了 ~/.tmux.conf.bash # 使用bash,主要是Android编译使用 按照tmu ...

  2. django之异常错误3(Student matching query does not exist.)

    错误提示: DoesNotExist at /blog/test2/ Student matching query does not exist. 说明:错误提示说明错误在test2中,查找数据库的表 ...

  3. Ubuntu常用命令大全[显示桌面]

    Ubuntu常用命令大全 查看软件xxx安装内容 #dpkg -L xxx 查找软件 #apt-cache search 正则表达式 查找文件属于哪个包 #dpkg -S filename apt-f ...

  4. java.lang.IllegalArgumentException: Minimum column number is 0

    easyUI的datagrid导出Excel时报如下错误: [2018-06-20 15:00:21] [ERROR] [org.jeecgframework.poi.excel.export.Exc ...

  5. Linux主要shell命令详解(下)

    命令行编辑操作 功能 Ctrl+b或左箭头键 左移一个字符(移至前一个字符) Ctrl+f或右箭头键 右移一个字符(移至后一个字符) Ctrl+a 移至行首 Ctrl+e 移至行尾 Esc b 左移一 ...

  6. webpack window 添加第三方库

    有的时候还想来点jquery,moment,undersocre之类的库,webpack可以非常容易的做到这一点,有谣言说Bower即将停止开发了, 作者推荐都使用npm来管理依赖.那么我们现在安装在 ...

  7. 如何在windows下安装JDK

    1:卸载 A:一定要删除注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft 项 B:最好用安装工具卸载JDK,如果没有的话就删除JDK文件夹然后用Wise Regis ...

  8. 利用hadoop来解决“共同好友”的问题

    假设A有好友B C D:B有好友A C D E:C有好友A B D E:D有好友A B C E;E有好友B C D. A -> B C D B -> A C D E C -> A B ...

  9. 【SqlServer】T-SQL的简介及基本用法

    一.T-SQL概述 SQL Server用于操作数据库的编程语言为Transaction-SQL,简称T-SQL.T-SQL与PL/SQL不同,并没有固定的程序结构.T-SQL包括以下4个部分: DD ...

  10. 【Servlet】web.xml中url-pattern的用法

    目录结构: contents structure [+] url-pattern的三种写法 servlet匹配原则 filter匹配原则 语法错误的后果 参考文章 一.url-pattern的三种写法 ...